我正在尝试在MATLAB中创建一个输入对话框,根据我从早期转换矩阵调用的数字填写默认答案。问题是,无论我做什么,我都无法将数字转换为字符串,这意味着我无法将它们称为defaultans
属性。 num2str
等基本转换不起作用,因为数据可能是负数。 char
似乎也不起作用。
earlierData = [ 1 -1.2 3 5 -0.2 4];
prompt = {'Enter x translation:', 'Enter y translation:', 'Enter z translation:', 'Enter x rotation:', 'Enter y rotation:', 'Enter z rotation:'};
name = 'Enter the values for the desired rotation matrix.';
num_lines = 1;
defaultans = [ox, oy, oz, oxrot, oyrot, ozrot];
nTransform = inputdlg(prompt, name, num_lines, defaultans);
newTranslate = [str2double(nTransform{1}) str2double(nTransform{2}) str2double(nTransform{3})];
nxrot = str2double(nTransform{4});
nyrot = str2double(nTransform{5});
nzrot = str2double(nTransform{6});
% make new transformation matrix
rot = makehgtform('translate', newTranslate, 'xrotate', nxrot,...
'yrotate', nyrot, 'zrotate', nzrot);
答案 0 :(得分:1)
num2str
在负数上运行得很好。您需要确保inputdlg
的默认值是字符串的单元格数组,而不仅仅是纯字符串。因此,您只需将num2str
的输出放在单元格数组中:
default = [1.1 2.2 3.3];
nTransform = inputdlg('Prompt', 'Name', 1, {num2str(default)});
现在加载输入的值:
numericValues = str2double(strsplit(nTransform{1}));
newTranslate = numericValues(1:3);
nxrot = numericValues(4);
nyrot = numericValues(5);
nzrot = numericValues(6);