How do I ascribe a variable to a cell?

Hi,
First of all thanks for reading this and trying to help :)
I am struggling with "attaching" my script to functions written by (many) others.
For that I need to take data out of a text file. In the function (not mine) the data later gets accessed using the command: soilstructure(1).thickness
I have tried with a table and a cell array and attributed the name 'thickness' to the last column, but it wont accept it as index, at least not using the above command... The error message reads: Struct contents reference from a non-struct array object.
Here is what I have so far:
layer = readtable('soil.txt',...
'Delimiter',' ','ReadVariableNames',false);
soilstructure = table2cell(layer,...
'VariableNames',{'type' 'radius' 'G' 'nu' 'rho' 'damping' 'thickness'});
x = soilstructure(1).thickness %why can't it access the data???
The table looks as follows:
soilstructure =
4×7 cell array
'L' [0] [6.8322e+06] [0.4900] [1840] [0.0500] [ 15]
'L' [0] [6.9898e+06] [0.4900] [1840] [0.0500] [ 15]
'L' [0] [2.5479e+07] [0.4900] [1840] [0.0500] [ 85]
'H' [0] [3.3425e+07] [0.4900] [1840] [0.0500] [NaN]
Thank you so much for your help
Chris

2 Comments

You forgot to attach 'soil.txt'. Please attach it for the best help.
Thank you for your time. I attached it. It is just an example file though the real files are much longer...

Sign in to comment.

 Accepted Answer

madhan ravi
madhan ravi on 14 Dec 2018
Edited: madhan ravi on 14 Dec 2018
The proper way to access it would be( see Accessing cell array for better understanding):
x = soilstructure{:,7} % but I don’t see your point of converting it to a cell from table but anyhow..
But if you want it to be treated as the way it was this is how I would do it:
remove soilstructure and replace it with (the reason for your error was it was a cell and you were treating it as a struct) the below , treats the data as a table as it was imported as a table
layer.Properties.VariableNames={'type'.... rest the same
layer.thickness % this will access the thickness column

8 Comments

Dear Madhan Ravi,
Thank you for your quick help!
The problem is that the many functions later on in the script retrieve the data using the command: layer(1).thickness (or layer(1).rho etc...)
So I really don't have any choice but to make that command useable. I don't understand why it wouldn't just give me the data from the row(1) and the variable: .thickness...
can you upload the data as a .mat file?
Sure thing. Thanks again for your time.
Try the below you had to use table2struct() instead of table2cell():
soilstructure = readtable('sample.txt',...
'Delimiter', ' ' ,'ReadVariableNames',false);
soilstructure.Properties.VariableNames = {'type','radius','G','nu','rho','damping','thickness'},...
{'char','double','double','double','double','double','double'};
soilstructure = table2struct(soilstructure);
soilstructure(1).thickness
Thank you very much that worked!
Still weird though since later on in the code it uses the command
d_ges=cell2mat(soilstructure(1).thickness);
implying that it is a cell array after all...
madhan ravi
madhan ravi on 14 Dec 2018
Edited: madhan ravi on 14 Dec 2018
Anytime :) , I severely doubt it because when you use dot indexing it means that the variable is a struct , I frankly don't know why cell2mat() has been used treating the variable as a cell.
It is very troubeling though since the program in it's entirety (we lost the input script and a few functions) apparently worked before. I guess I will have to change the parts that treat the struct as a cell and solve the issue that way. Can't seem to understand how it worked before though...
Again many thanks for you help Madhan!
Have a great day
Chris
madhan ravi
madhan ravi on 14 Dec 2018
Edited: madhan ravi on 14 Dec 2018
You're welcome Chris , I suggest you to contact the person who coded the script so that you know what he/she mean by this.

Sign in to comment.

More Answers (1)

Mark Sherstan
Mark Sherstan on 14 Dec 2018
Edited: Mark Sherstan on 14 Dec 2018
It is a cell array not a structure. Retrieve the information as follows:
x = soilstructure{1,7}

Categories

Products

Community Treasure Hunt

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

Start Hunting!