Hi everyone, I have a problem related to the excel data opening in Matlab. I have a document in excell, in which I have a list of parameters with te name of the parameters, their unit, their numerical value and their description. Since I want this parameters in matlab, I import the data from excel to matlab (I have no problem with that). I use the following command:
[a,b]=xlsread('Parameters_excel');
With that command, I get an 'a' vector with the numerical values of the parameters and a 'b' matrix with all the text of the excell.
a =
1.0e+13 *
0.000000034700000
0.000000000008935
0.000000000540000
0.000000000053412
0.000000000009700
0.000000001240000
0.000000000006300
0.000000000009000
0.000000000012000
0.000000000000940
0.000000000102500
0.000000000000123
b =
'Mplatf' 'kg' '' 'Platform's mass'
'GMplatf' 'm' '' 'Platform's center off mass'
'Jrotor' 'kg*m^2' ''
'Jgen' 'kg*m^2' ''
'Ngbr' '' '' 'Gearbox ratio'
'Arotor' 'm^2' '' 'Rotor's area'
'Rrotor' 'm' '' 'Rotor's radio'
'rt' 'm' '' 'Hub height'
'Zbuoy' 'm' ''
'Dbt' 'm' '' 'Diameter of buoy cilinder'
'ro_w' 'kg/m^3' '' 'Water density'
'ro_a' 'kg/m^3' '' 'Air density'
Now the problem is that I have only parameter's value without the name of them, I mean, instead of Mplatf=347000 I have a(1,1)=347000. What I want is to create variables with the name and value of the parameters.I mean, I would like to have the following variables:
Mplatf=347000, GMplatf=-89.92...
Is there any way to do it automatically?

 採用された回答

Stephen23
Stephen23 2016 年 5 月 10 日
編集済み: Stephen23 2016 年 5 月 11 日

2 投票

Solution
It is trivial to convert the data into a structure:
>> C = [b(:,1)';num2cell(a)'];
>> S = struct(C{:});
>> S.Jrotor
ans =
5400
>> S.rt
ans =
90
>> S.Mplatf
ans =
347000
>> S.GMplatf
ans =
89.35
Good Advice
Do not try to construct lots of separate variables from data: no matter how much beginners love it, dynamically defining or accessing variables names is slow, buggy, and horrible code:
Either put the data in one structure, or one table, or one cell array... do you see the pattern there?

1 件のコメント

Iosu
Iosu 2016 年 5 月 11 日
Thank you very much.

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

その他の回答 (1 件)

Pablo Shi
Pablo Shi 2020 年 7 月 27 日
編集済み: Pablo Shi 2020 年 7 月 27 日

0 投票

You can also use the command
help assignin
But IMO, @Stephen Cobeldick is using a better data structure.

質問済み:

2016 年 5 月 10 日

編集済み:

2020 年 7 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by