Need to convert char to struct

I am trying to use a trading algorithm and I need to check prices daily. I can download the prices manually and load them into matlab manually, but I don't want to do that every single day. I get the data from quandl.com and when I use urlread on the data file, it returns a char of the data with each value separated with a comma. How can I convert this to a structured array in my program? Should I be using a different command besides urlread for this? I've tried using struct() on the char variable but is doesn't work.

답변 (2개)

Walter Roberson
Walter Roberson 2014년 4월 13일

0 개 추천

I recommend using regexp() with the 'split' option to get the text for each number into its own cell array entry, and then str2double() the overall cell array to get the numeric values. Or if you know exactly how many data values are expected, you could sscanf() on the strings.

댓글 수: 3

Paul
Paul 2014년 4월 13일
Is this right?
ewa2 = regexp(ewa, 'split'); ewa3 = str2double(ewa2);
"ewa" is my char variable. The code above returns ewa2 as empty and ewa3 as nan.
ewa2 = regexp(ewa, ',\s*', 'split');
ewa3 = str2double(ewa2);
ewa = urlread('http://www.quandl.com/api/v1/datasets/GOOG/NYSEARCA_EWA.csv?column=4');
ewa2 = regexp(ewa, ',\s*', 'split');
ewa3 = str2double(ewa2);
This gives me a 1 x 4098 double of NaN's as ewa3. You can look at the data I'm dealing with by opening the link: http://www.quandl.com/api/v1/datasets/GOOG/NYSEARCA_EWA.csv?column=4

댓글을 달려면 로그인하십시오.

Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 13일

0 개 추천

str='12.03,23,25,26,30'
a=struct('data',num2cell(str2double(regexp(str,'[\w\.]+','match'))))

댓글 수: 4

ewa = urlread('http://www.quandl.com/api/v1/datasets/GOOG/NYSEARCA_EWA.csv?column=4');
ewa2=struct('data',num2cell(str2double(regexp(ewa,'[\w\.]+','match'))))
This woudld work, but for some reason it not only seperates at commas, but also at hyphens, which seperates the dates, which look like this: 2014-4-11
Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 13일
편집: Azzi Abdelmalek 2014년 4월 13일
a=regexp(ewa,'[^,\s]+','match')
str=reshape(str,2,[])'
dates=str(2:end,1)
price=str2double(str(2:end,2))
Image Analyst
Image Analyst 2014년 4월 13일
I think you mean "dates" since date is the name of a built in function.
Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 13일
Exact

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2014년 4월 13일

댓글:

2014년 4월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by