Reading a csv file
Afficher commentaires plus anciens
Hello Matlab community! I'm a begginer at Matlab, and I was given a project to complete. There's a few more steps to it, but right now I'm a little stuck on one particular part. I'm trying to read variables and their values from an easily readable csv file. It needs to be easy to read and change because people will be changing the values. This is what the csv file contains:
Orbital Input Variables, Values, Units
Altitude, 841, km
OrbitalType, 0,
DesiredStaticGSD, 500,
ForwardMotionCompensationFactor, 1,
DetectorSpatialChannels, 2800,
FNumberTelescope, 1, #
RadiusEarth, 6378100, m
I will be using other similar csv files in my code. The code that I have right now is this:
T = readtable('OrbitalInputs.csv','HeaderLines',0,'ReadVariableNames',true);
display(T);
MyStruct.Remark = 'These are my imported variables';
for Count=1:size(T,1)
A=table2cell(T(Count,1));
B=table2cell(T(Count,2));
MyStruct.(A{1}) = B{1};
end
But I find this code a bit confusing and circular. Is there any easier way to do the same thing in an easier code? Or something more direct?
I'm also trying to take the variables imported from the csv file and assign the values of those variables to other variables in a different code I have, so that I can use them in some calculations. Are there any specific ways to do this or any suggestions for how I would go about that?
Thank you!
Réponse acceptée
Plus de réponses (2)
dpb
le 16 Avr 2019
You started off well w/ the readtable route...but, once you've got the table, then use the variables there, don't create new ones, particularly ones with inscrutable names regarding their purpose...
>> tOrbit=readtable('cordelia.csv','ReadRowNames',1); % read the file as a table w/ variable as row ID
>> tOrbit.Units=categorical(tOrbit.Units) % convert units to a categorical variable
tOrbit =
7×2 table
Values Units
__________ ___________
Altitude 841 km
OrbitalType 0 <undefined>
DesiredStaticGSD 500 <undefined>
ForwardMotionCompensationFactor 1 <undefined>
DetectorSpatialChannels 2800 <undefined>
FNumberTelescope 1 #
RadiusEarth 6.3781e+06 m
>> tOrbit('RadiusEarth',:) % access all of the given row--another table
ans =
1×2 table
Values Units
__________ _____
RadiusEarth 6.3781e+06 m
>> tOrbit('RadiusEarth','Values') % access one variable -- also returns a table
ans =
table
Values
__________
RadiusEarth 6.3781e+06
>> tOrbit{'RadiusEarth','Values'} % dereference to the underlying data type with {}
ans =
6378100
>>
An alternate way to do something similar altho the units field compounds the effort is to make the file in the form that it could be executed as an m-file (or the text read and interpreted) by writing the data lines as valid Matlab assignments. This does require more discipline upon the user to ensure the line is indeed valid code which may be too much to expect/demand.
As far as passing data to other routines, use argument lists and functions to pass the data -- then the calling routine can use whatever names it has independent of the called function as far as actual name; just have to have a defined interface for the routine/function.
Jeremy Hughes
le 16 Avr 2019
0 votes
It's hard to say without know what you'll be doing with the data, but I'd reccomend using the table directly unless you need a different container. Most importantly, you should avoid looping over the rows.
e.g. T.Values
You can also assign your remark to
T.Properties.Description = 'These are my imported variables';
1 commentaire
Cordelia David
le 16 Avr 2019
Catégories
En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!