Why and how can i fix the vert cat error in this code? It works fine with a 3 len string but over that i get a vert cat error

function PeaksFlare_Callback(hObject, eventdata, handles)
global s;
set(handles.text12,'String','');
for w=1:length(s)
e=get(handles.text12,'String');
e=[e;num2str(w),'. ',num2str(s(w))];
set(handles.text12,'String',e);
end
set(handles.text11,'visible','on');
set(handles.text12,'visible','on');

 採用された回答

You set "e" as a string, and then you try
e=[e;num2str(w),'. ',num2str(s(w))];
This tries to put (num2str(w),'. ',num2str(s(w))) as a new row in "e". How long is that string going to come out? If it doesn't happen to come out as exactly the same number of columns as "e" already is, you are going to have a problem.
You can change the code as
for w=1:length(s)
e = cellstr( get(handles.text12,'String') );
e{end+1} = [num2str(w),'. ',num2str(s(w))];
set(handles.text12,'String',e);
end

その他の回答 (1 件)

What value of s works, and what value of s breaks? Is s a string or an array of numbers?
You need to ensure that length(s) is less than 10 for this code to work. If s is not a string, you need to ensure that num2str(s(w)) is always the same length. You might consider using sprintf and specify padding etc in the format specifier.
eg
e = [e; sprintf('%2d. %c', w, s(w))]; % s is string
e = [e; sprintf('%2d. %-3d', w, s(w))]; % s is up to 3-digit int.
% etc....
Also, I would avoid the constant set and get calls, when you can do it with one set. Who knows - maybe that is what's causing problems:
e = [];
for w=1:length(s)
e = [e;num2str(w),'. ',num2str(s(w))];
end
set(handles.text12,'String',e);
Hope that helps.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by