MATLAB中的数据序列
我在MATLAB中有数据序列:data1,data2,data3,。。。。我想一个接一个地把它们打印出来。我被困在那上面了MATLAB中的数据序列,matlab,Matlab,我在MATLAB中有数据序列:data1,data2,data3,。。。。我想一个接一个地把它们打印出来。我被困在那上面了 clear; clc; data1=[1,2]; data2=[3,4]; n=[1,2]; for i=1:length(n) fname(i,:)=sprintf('data%d',n(i)); end 这对你有用吗 clear; clc; data1=[1,2]; data2=[3,4]; n=[1,2]; for i=1:length(n) eva
clear; clc;
data1=[1,2];
data2=[3,4];
n=[1,2];
for i=1:length(n)
fname(i,:)=sprintf('data%d',n(i));
end
这对你有用吗
clear; clc;
data1=[1,2];
data2=[3,4];
n=[1,2];
for i=1:length(n)
eval(['data' num2str(n(i))])
end
这对你有用吗
clear; clc;
data1=[1,2];
data2=[3,4];
n=[1,2];
for i=1:length(n)
eval(['data' num2str(n(i))])
end
不使用
eval
的可能解决方案如下
基本上包括以下步骤:
- 获取工作区中的数据列表(使用
功能)who
- 识别要打印的数据(使用
识别表单regexp
的变量名称,其中datax
是一个数字)x
- 将这些变量保存在临时
文件中.mat
- 将
文件加载到只允许打印变量的结构中.mat
- 利用
访问变量动态字段名
% Define some data
data1=[1,2];
data2=[3,4];
data3=rand(5)
data4a=rand(5)
dataaaa3=rand(5)
var_1=1
b=2
% Get the list of data in the Workspace
str=who
% Identify the data to be printed
var_to_print=regexp(str,'data\d$')
idx=~cellfun(@isempty,var_to_print)
% Down select the variables to be printed
str{idx}
% Generate a temporary ".mat" filename
tmp_name=[tempname '.mat']
% Save the data to be printed in the temporary ".mat" file
save(tmp_name,str{idx})
% Load the data to be printed into a struct
v=load(tmp_name)
% Get the names of the varaibles to be printed
f_name=fieldnames(v)
% print the value of the variables
for i=1:length(f_name)
[char(f_name(i)) ' = ']
v.(f_name{i})
end
% Move the temporary ".mat" file in the recycle folder
recycle('on')
delete(tmp_name)
希望这有帮助
Qapla’不涉及使用
eval
的可能解决方案如下
基本上包括以下步骤:
- 获取工作区中的数据列表(使用
功能)who
- 识别要打印的数据(使用
识别表单regexp
的变量名称,其中datax
是一个数字)x
- 将这些变量保存在临时
文件中.mat
- 将
文件加载到只允许打印变量的结构中.mat
- 利用
访问变量动态字段名
% Define some data
data1=[1,2];
data2=[3,4];
data3=rand(5)
data4a=rand(5)
dataaaa3=rand(5)
var_1=1
b=2
% Get the list of data in the Workspace
str=who
% Identify the data to be printed
var_to_print=regexp(str,'data\d$')
idx=~cellfun(@isempty,var_to_print)
% Down select the variables to be printed
str{idx}
% Generate a temporary ".mat" filename
tmp_name=[tempname '.mat']
% Save the data to be printed in the temporary ".mat" file
save(tmp_name,str{idx})
% Load the data to be printed into a struct
v=load(tmp_name)
% Get the names of the varaibles to be printed
f_name=fieldnames(v)
% print the value of the variables
for i=1:length(f_name)
[char(f_name(i)) ' = ']
v.(f_name{i})
end
% Move the temporary ".mat" file in the recycle folder
recycle('on')
delete(tmp_name)
希望这有帮助
qapa'您是否尝试过
disp(data1)
?当我disp(data1)
时,它会显示矩阵,但我想显示我拥有的所有矩阵,并且我希望它按一个周期显示,因为它们很多都像70,并且disp(fname(1,:)
不起作用,它只显示数据1。为什么您会有70个独立的数据变量,而不是一个可以轻松循环的二维数组(或单元格数组,如果变量长度不同)?您是否尝试过disp(data1)
?当Idisp(data1)
它显示矩阵时,我想显示我所有的矩阵,我希望它按一个周期显示,因为它们很多像70,而且disp(fname(1,:)
不起作用,它只显示数据1。为什么要有70个单独的数据变量,而不是一个二维数组(或者单元格数组,如果变量长度不同)你可以很容易地循环吗?如果答案令人满意,请接受答案。当然,它只想让我等2分钟。再次感谢,这正是我想要的。如果答案令人满意,请接受答案。当然,它只想让我等2分钟。再次感谢,这正是我想要的。你也可以做save(tmp_name,'data*')
@Sueversave(tmp_name,'data*')
也会保存变量,比如data4a
,dataaaa3
(参考,我答案中的变量),而OP说变量名是data1
,data2
。啊,是的,很好。您也可以执行保存(tmp_名称'-regexp','^data\d+$)
@Suever nice solution!我是regexp
的初学者。谢谢你的回答。我可能会使用这个临时的.mat
文件。你也可以做保存(tmp_name,'data*')
@Suever保存(tmp_name,'data*')
也会保存变量,比如data4a
,dataaaa3
(参考,我答案中的变量),而OP说变量名是data1
,data2
。啊,是的,很好。您也可以执行保存(tmp_名称'-regexp','^data\d+$)
@Suever nice solution!我是regexp
的初学者。谢谢你的回答。我可能会使用这个临时.mat
文件。