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

조회 수: 2 (최근 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에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by