How to save a matrix from an object with unknow name?

조회 수: 3 (최근 30일)
Juan Sepúlveda
Juan Sepúlveda 2020년 10월 31일
댓글: Juan Sepúlveda 2020년 10월 31일
I have a file with around 50 variables, each with different names, so I have their names contained in a cell array called filtered_vars.
Then in order to load them I was using
for i=1:1%size(filtered_vars,1)
temp=load('merged_t7_original',filtered_vars{i})
end
temp =
struct with fields:
N_force_wind_to_wave_Nmin: [1444202×672×2 single]
Now my problem is that the variable temp corresponds to a structure object with the name of the variable which was loaded, and then I don't know how to read it in order to save it into a matrix.
  댓글 수: 2
Adam Danz
Adam Danz 2020년 10월 31일
I assume this isn't the actual code you're using since 'temp' would be overwritten on each iteration.
What are the values being loaded?
Are they are numeric scalars? Are they vectors?
Do they contain mixed data types where some are strings|cell arrays|object handles, etc?
Are you trying to store all of the values into a different type of an array?
Do you want to keep all of the variables or are you just searching for some of them?
Lots of unknowns.....
Juan Sepúlveda
Juan Sepúlveda 2020년 10월 31일
Hi Adam, Thank you for your answer, I'm sorry about the lack of information, the problem is that I don't have a clue what is on the matrices, they are numbers, but different types in each of the matrices, basically I need to take the column "j" in each of them, complementing it with the following answer given by drummer, I'm able to fix my problem, the magic command was "cell2mat(struct2cell(temp))" and the code will look like this:
load('merged_t7_original','t7_info')
j=min(find(contains(t7_info.N.Unique,'B_60AQ')==1))
full_vars=who('-file','merged_t7_original');
filtered_vars=full_vars(contains(full_vars,'Nmin'));
for i=1:size(filtered_vars,1)
temp=load('merged_t7_original',filtered_vars{i});
temp=cell2mat(struct2cell(temp));
data_{i}=temp(:,j,1);
end

댓글을 달려면 로그인하십시오.

채택된 답변

drummer
drummer 2020년 10월 31일
Try
yourMatrix = cell2mat(struct2cell(YourStructure));
Cheers
  댓글 수: 1
Juan Sepúlveda
Juan Sepúlveda 2020년 10월 31일
It was exactly what I needed, thank you very much drummer!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by