How to extract values from 1x1 cell array

I've been searching around for a solution to this, there are plenty of people who ask similar questions but none of the solutions have worked for me.
See the attached csv-file, the code I have so far is this;
stock_history = importdata("stock_history.csv");
for i=1:length(stock_history)
row_i = stock_history(i,:);
end
With this code, the variable row_i will be a 1x1 cell array, where the first value is
{'"69","2018-11-30","185.35000","185.65000","183.35000","184.70000","1482674","2018-12-06 11:12:30"'}
In the same for-loop, I would like to save the 1st, 2nd, 3rd, 6th values. So in the example above I would save the values 69, 2018-11-30, 185.35000, 184.70000. I would like to save them into a regular list, for example a list called c_id would save all values from the 1st spot in the cell array, date would be a list with the 2nd values, open_price and close_price would be the 3rd and 6th values.

 Respuesta aceptada

madhan ravi
madhan ravi el 23 de Dic. de 2018

1 voto

Just use readtable() to read the file

9 comentarios

Thanks, but that doesn't solve the issue. I've tried using table and extracting it into a list with this;
stock_history = readtable("stock_history.csv")
c_id = stock_history{:,1}
But c_id will become an array instead of a regular list
madhan ravi
madhan ravi el 23 de Dic. de 2018
upload a short sample of your desired output
Aram Eskandari
Aram Eskandari el 23 de Dic. de 2018
not sure how to upload it since I don't have it, but I might be able to explain it a bit better;
if you use readtable("stock_history.csv"), you will get a table with 8 columns/variables. I would like the 1st column to be saved as a list/vector of integers, the 2nd column I want to save as a vector of strings, the 3rd and 6th columns I want to save as a list/vector of doubles.
str2double(T{:,1}) % double array
string(T{:,1}) % string array
Aram Eskandari
Aram Eskandari el 23 de Dic. de 2018
Thank you so much, this worked perfectly!
madhan ravi
madhan ravi el 23 de Dic. de 2018
Anytime :)
Guillaume
Guillaume el 23 de Dic. de 2018
"But c_id will become an array instead of a regular list"
There is no list type in matlab, so what is a regular list?
By default readtable interpret quoted text as text. If you want to ignore the quotes and get numbers you either need to tell readtable or easier, let matlab figure it out using detectImportOptions:
opts = detectImportOptions('stock_history.csv');
stock_history = readtable('stock_history.csv', opts);
c_id = stock_history{:, 1};
c_id will be a double column vector.
madhan ravi
madhan ravi el 23 de Dic. de 2018
+1 @Guillaume

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2018a

Preguntada:

el 23 de Dic. de 2018

Comentada:

el 23 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by