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

1 次查看(过去 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.

回答(2 个)

Harsh Saxena
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!

Image Analyst
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:

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by