How to selectively extract information from a matrix/MAT file in MATLAB 8.1 (R2013a) ?

1 view (last 30 days)
I would like to selectively extract text strings present in different rows, from a variable inside MAT file(saved to workspace from UI(GUI/App)) and corresponding to these text strings I want to fetch different column contents for the same row. This is in regard of the fact that I need to extract particular string, whose position keep changing from file to file.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 25 Oct 2013
In order to selectively extract text strings present in different rows and access different column contents corresponding to these rows. In order to do so you can try with the sample script shown below:
var1=struct('x',{'team','qualification','engineering','management','leader'},'y',{'1','2','3','4','5'}) % create a '1 by 5' structure which has 2 variables x and y.
save('var1Mat.mat','var1') % save the above structure as MAT file
var2=struct('x',{'qualification','team','leader','engineering','management'},'y',{'6','7','8','9','10'}) % create another '1 by 5' structure which has 2 variables x and y.
save('var2Mat.mat','var2') % save the above structure as MAT file
ReVar1=load('var1Mat.mat')
temp = fieldnames(ReVar1); % In order to access variable inside structure
ReVar1 = ReVar1.(temp{1});
tempvar1={ReVar1(1:end).x} %obtain the 1st variable x for all the indices from var1 MAT file.
pos1=strcmp(tempvar1(1,:),'team')% compare this with the string and get the position for all matching instances.
ReVar1(pos1).y % display corresponding position variable y from the same MAT file
ReVar2=load('var2Mat.mat')
temp = fieldnames(ReVar2); % In order to access variable inside structure
ReVar2 = ReVar2.(temp{1});
tempvar2={ReVar2(1:end).x} % obtain the 1st variable x for all the indices from var2 MAT file.
pos2=strcmp(tempvar2(1,:),'team')% compare this with the string and get the position for all matching instances.
ReVar2(pos2).y %display corresponding position variable y from the same MAT file
Upon executing this script you can observe it fetches the value of y for different positions of string 'team'.

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products


Release

R2013a

Community Treasure Hunt

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

Start Hunting!