data import from mat file

I have a mat file named as xyz.mat, the mat file contains 10 variables named as a_1, a_2, a_3......a_10.
1. I want to develop a script that will load this mat file.
2. Load the variables and record their number 1 to 10 automatically.
3. since there are 10 variable (a_1 to a_10) , a function abc will be run 10 times, one time for each variable , the result after processing of each variable will be stored in varaibles R_1 to R_10. The script should be a generic one it will remain valid for even more than 10 variables.

2 Comments

jibs - what have you tried so far? Please give us an idea of what you have attempted. Also, strongly consider not using variables that are numbered and use an array (perhaps cell) instead to manage all of the data.
Actually I'm using a simple gui, having single push button (Load_mat File). By pressing it a mat file is loaded.
The mat file has now 10 variables i.e (a_1 to a_10). It could have more than 10 variables as well. I want all of them to be loaded one by one progamitically after that a function y = abc , will be executed on all variables a_1 to a_10 and their result will be saved in variables R_1 to R_10.

Sign in to comment.

Answers (1)

instead of a_1 ,a_2,..., define a vector "a" where its elements a(1), a(2), ... are your input argument. your function should look like this:
function R = abc(a)
% write your function
end
now for your script:
load('xyz.mat') % load input data a
R = abc(a) % call function abc and calculate R
save('Results.mat','R') % save R to the file Results.mat

8 Comments

Actually I'm using a simple gui, having single push button (Load_mat File). By pressing it a mat file is loaded.
The mat file has now 10 variables i.e (a_1 to a_10). It could have more than 10 variables as well. I want all of them to be loaded one by one progamitically after that a function y = abc , will be executed on all variables a_1 to a_10 and their result will be saved in variables R_1 to R_10.
if you have saved all a_1, a_2 ,... in a mat file, once you load the file; all of them will be loaded and you don't need to load each of them one by one. as I said it's best to store all of them in one array but if you need to load them one by one, do a function to them one by one and store the results one by one, you could do sth like this:
abc = @(x) x^2; % function
n = 10; % number of i/o: a_1 , ... ,a_n
for it = 1:n
load('a.mat',sprintf('a_%d',it)) % load a_i
eval(sprintf('R_%d = %d',it,abc(eval(sprintf('a_%d',it))))) % R_i = abc(a_i)
end
str = sprintf('''R_%d'',',1:10); str(end)='';
str = ['save(''Results.mat'',',str,')'];
eval(str) % save('Results.mat','R_1',...'R_n')
again. this method is not recommended. use arrays if not using matrices.
here u have hard coded n=10, which is not required.
I want while loop to be terminated when the next variable after a_10 , say a_11 does not exist using exist (a_11) function.
I know once the mat is loaded there is no need to load all variable cz they all will be in access. the problem is to progamatically do the function evaluation every time and store result at some place.
so I guess if you replace it with while loop, the job is done. is there any other problem that this code won't do?
% I have done it in this way pl suggest any improvement,
% the size of r is being changed on every iteration
% how do i control it??
clear all;
clc;
load('As.mat');
r=[ ];
t=1;
count =0;
while t==1
count= count +1;
d = strcat ('a_', num2str(count));
t = exist (eval('d'));
if (t == 1);
a = mean (eval(d));
disp(a)
r(1,count)=a;
else
return
end
end
the only way to avoid size change in a loop is preallocation. before the loop add a "r=zeros(1,n)". and "n" must be known. it is weird that the number of inputs are unknown. how is it that you can't preallocate it? you can even ask the user to enter the number of inputs. if there is no way for preallocation, changing size on each iteration is not very bad, it is just a little slow.
k = who('-file','As.mat','a*');
r=zeros(1,length(k));
% % just added these two lines and done it
correct.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 10 Aug 2020

Commented:

on 11 Aug 2020

Community Treasure Hunt

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

Start Hunting!