排序杀死程序:MATLAB
Sort kills the program : MATLAB
我编写了以下程序,以便从用户那里获取字符串数组,对它们进行排序,然后将排序后的数组显示给用户。
但似乎 sort
函数完全杀死了程序。因为第二个msgbox
从来没有出现过。
另请注意,如果我在 inputdlg
第一次出现时将其取消,则会出现两个消息框。
strings = {};
count = 1;
while(1)
prompt = {strcat('Enter the ', num2str(count), '# String')};
temp = inputdlg(prompt,'Input String',1,{'String'});
if isempty(temp)
break
end
strings{count} = temp;
count = count + 1;
end
msgbox('Processing....');
sorted = sort(strings); % The program stops executing on this point
msgbox('Operation Completed');
那么为什么程序从不执行最后一行?谢谢。
变量 temp
是一个包含输入字符串的 1x1 单元格。通过分配 strings{count} = temp
,strings
的每个条目都是一个 1x1 单元格。如果您查看工作区中的变量,您将能够验证这一点。您只能通过调用
访问输入的字符串
strings{count} = temp{1};
通过此修改,strings
单元格包含字符串而不是单元格。现在您将能够使用 sort
函数对单元格进行排序,您的代码应该会按预期工作。
我编写了以下程序,以便从用户那里获取字符串数组,对它们进行排序,然后将排序后的数组显示给用户。
但似乎 sort
函数完全杀死了程序。因为第二个msgbox
从来没有出现过。
另请注意,如果我在 inputdlg
第一次出现时将其取消,则会出现两个消息框。
strings = {};
count = 1;
while(1)
prompt = {strcat('Enter the ', num2str(count), '# String')};
temp = inputdlg(prompt,'Input String',1,{'String'});
if isempty(temp)
break
end
strings{count} = temp;
count = count + 1;
end
msgbox('Processing....');
sorted = sort(strings); % The program stops executing on this point
msgbox('Operation Completed');
那么为什么程序从不执行最后一行?谢谢。
变量 temp
是一个包含输入字符串的 1x1 单元格。通过分配 strings{count} = temp
,strings
的每个条目都是一个 1x1 单元格。如果您查看工作区中的变量,您将能够验证这一点。您只能通过调用
strings{count} = temp{1};
通过此修改,strings
单元格包含字符串而不是单元格。现在您将能够使用 sort
函数对单元格进行排序,您的代码应该会按预期工作。