generate and save numerous datas in one code then load them in another code
1 次查看(过去 30 天)
显示 更早的评论
Hi there, if I have two code 'A.m' and 'B.m' under the same file. In 'B.m' I will generate a few variables. I want to save those variables and then load them in 'A.m'. For example, I have p=randi([-5,5]); q=randn(3,2) in 'B.m', somehow I save them, then in 'A.m', after load, the p and q will appear in 'A.m' directly and can use them directly, just like after load , in the next line I can operate 'n=p+q'.
0 个评论
回答(1 个)
VBBV
2023-1-10
% in A.m file
[p,q] = B % call the function wbich returns those outputs variables (values)
n = p + q % add them directly as you wanted
% in B.m save this in another m file with same name (say) B and in same
% folder
function [p,q] = B
p = randi([-5,5]);
q = randn(3,2);
end
3 个评论
VBBV
2023-1-11
编辑:VBBV
2023-1-11
In that case, you can load the data files( preferably *.mat) into workspace using a for loop
for k = 1:length(Files)
K{k} =
load(sprintf('file%d.mat',k));
end
K is an array of structures which contains data fields from each individual data file. This can accessed conveniently inside another loop in the main function including data present its fields. Hope it helps. Files is the number of data(mat) files present accessed.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!