Why does this give me the "Unable to perform assignment because the left and right sides have a different number of elements." error?

1 view (last 30 days)
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.

Answers (2)

Harsh Saxena
Harsh Saxena on 5 Jun 2023
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!

Image Analyst
Image Analyst on 6 Jun 2023
Edited: Image Analyst on 6 Jun 2023
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:

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!