assign variable from txt-file

Hello,
i want to realize the following issue...
I have a given txt-file in such a format:
var1 = a
var2 = b
var3 = c
Now i want to assign these variable in matlab such as var1 = a and var2 = b ... But the vectors a, b, c, d, e and f are part of a big struct, where i can access on each value with that command: data(i).a(j), where i is the struct number and j the position of the value in f.e the vector a.
So if I change my txt-file to:
var1 = d
var2 = e
var3 = f
... I need the values of d in variable var1 which i use in matlab. So in the text-file it is a string but in my matlab program i have to use the string 'd' as a variable name to access the variable d from my struct. After the assignment i want to fill it like that, but i have to use something other than data(i).d(j), because i want the assignment dependant on my txt-file
for i=1:15
for j=1:length(var)
var(:,1) = data(i).d(j);
var(:,2) = data(i).e(j);
var(:,3) = data(i).f(j);
end
end
I hope I've explained my issue quiet understandable and I thank you in advance for any help...
Best regards
Da Ke

6 件のコメント

Walter Roberson
Walter Roberson 2019 年 1 月 21 日
You can use fieldnames() to find the names of the variables you received, and you can use dynamic field names to access.
fn = fieldnames(data);
var(:,1) = data.(fn{1});
Image Analyst
Image Analyst 2019 年 1 月 21 日
I think you forgot to attach your text file. I'm not really going to put effort into answering this until I see the actual file. For all I know you can just rename the file extension to .m and run it as an m-file.
Daniel Kern
Daniel Kern 2019 年 1 月 21 日
Ahh thanks... that was a big step forward :)
But how i can search for a certain variable in my fieldname, cuz with the command
var(:,1) = data.(fn{1}); % is working...
var(:,1) = data.(fn{2}); % is working...
var(:,1) = data.(fn{'a'}); % something like that, but its not working...
I just can get the 1st or 2nd data/vector in my struct. Can i also search for a given string in my definied fieldname, if my struct has for example a vector called 'a' (and has 1000x1 double) ?
And in my case i take the string 'a' from my txt-file. So i have a 1x1 cell containing 'a'
My txt-file:
matlab variable ; struct variable
var1 ; a
var2 ; b
filename= fullfile('C:MATLAB\data.txt');
data = load('my_struct.mat');
T= readtable(filename, 'Delimiter', ';', 'ReadRowNames', true);
new_variable1=T{'var1', :}; % gives a in 1x1 cell
new_variable2=T{'var2', :}; % gives b in 1x1 cell
new_var2=T{1,1}; % same result as above
fn = fieldname(my_struct);
var(:,1) = my_struct.(fn{???}); % search for a string in fn ?
Thanks in advance...
Daniel Kern
Daniel Kern 2019 年 1 月 21 日
the txt-file is in the attachment
Daniel Kern
Daniel Kern 2019 年 1 月 21 日
I tried that command but it return a cell array with a length dependant on the length of the string
var(:,1) = fn('a');
So for the case if my variable is not 'a' but 'time' it gives a 1x4 cell because 'time' has 4 letters...
Hmm :(
Walter Roberson
Walter Roberson 2019 年 1 月 22 日
If you have the data stored as a struct and you know the field name, then data.a . However since that is what you started with, I seem to be missing something.
If you have a cell array in which the first column is a variable name and the second is the corresponding value, then
var_a = YourCell{ strcmp(VariableYouAreLookingFor, YourCell(:,1)), 2};
In the case that the variable is not found, this would be empty. You would probably not want to store directly into an array because you would want to test for the variable being present and having the right size and datatype before storing.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeVariables についてさらに検索

質問済み:

2019 年 1 月 21 日

コメント済み:

2019 年 1 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by