Why does this give me the "Unable to perform assignment because the left and right sides have a different number of elements." error?
    조회 수: 8 (최근 30일)
  
       이전 댓글 표시
    
Hi,
I'm trying to convert my code into an app as an exercise. I'm trying to get a list to update its items to contain all components.
function CreateComponentButtonPushed(app, event)
            new_comp = comp(app.ComponentNameEditField.Value, length(app.components)+1, app.MolarMassgmolEditField.Value);
            new_comp.setAntoine([app.AntoineEqnAEditField.Value, app.AntoineEqnBEditField.Value, app.AntoineEqnCEditField.Value]);
            app.components = [app.components, new_comp];
            display_names = zeros(length(app.components), 1);
            for i = 1:length(display_names)
                %Set the current value to the name of the component
                display_names(i) = app.components(i).name;
            end
            disp(display_names);
            %Supposed to change the list in the app to contain all the new
            %display names
            app.ComponentsListBox.Items = display_names;
end
This is throwing up an error in the display_names(i) = app.components(i).name line. What's the issue here?
Thanks in advance.
댓글 수: 0
답변 (2개)
  Harsh Saxena
      
 2023년 6월 5일
        Hi, 
Maybe this error is occuring due to the fact that app.components.name(i) is a character array, so MATLAB considers left hand side as a double value while the right hand side as an array of characters. Try initializing the display_names as:
display_names = strings([length(app.components), 1]);
Hope this helps!
댓글 수: 0
  Image Analyst
      
      
 2023년 6월 6일
        
      편집: Image Analyst
      
      
 2023년 6월 6일
  
      Perhaps make it a cell array:
numControls = length(app.components)
display_names = cell(numControls, 1);
for k = 1 : numControls % Don't use i as an iterator.
    % Set the current value to the name of the component
    display_names{k} = app.components(k).name; % Use braces, not parentheses.
end
See the FAQ:
If you have any more questions, then attach your .mlapp file, and any data needed at run time, with the paperclip icon after you read this:
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


