textscan: strings and variables

Hi,
I have some csv file which contains numbers and strings (example below)
The column headers are in seperate file from the data, but can be merged to the same one.
I'd like to import the data with textscan (as I have old version of MATLAB-R14) and assign it to some variable (which is the column name) for further analysis.
How can I do that?
For numbers I use something like
eval (column name = [column data])
For time: as it with same length - I also don't have any issue (as I can split the data into 3 coulns and later handle it with time related functions).
But what about other ones where the string values are with variable length (like RATE or STATUS columns as in the example)?

댓글 수: 3

Stephen23
Stephen23 2021년 12월 18일
편집: Stephen23 2021년 12월 18일
Much more robust approaches:
  • use READTABLE (then the variable/column names can be almost anything)
  • use a structure (then the fieldnames can be any valid MATLAB name)
The current approach is inefficient, liable to bugs, turns of many code-helper tools, and is fragile: consider what would happen if the header text is empty or "QUIT" or "1" or "A-B".
Such fragile approaches are best avoided.
michael
michael 2021년 12월 18일
@Stephen is READTABLE available in R14?
I need the solution to be fast enought as my tables are with many colkumns and hundred of thouthand of rows.
Walter Roberson
Walter Roberson 2021년 12월 18일
https://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/
Dynamic field names existed in your release (R13 introduction), and for earlier releases use setfield()

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

답변 (1개)

Stephen23
Stephen23 2021년 12월 18일
편집: Stephen23 2021년 12월 18일

1 개 추천

Perhaps something like this, a more robust approach using a structure (this still requires that the headers are valid MATLAB names):
opt = {'Delimiter',','};
fid = fopen('test.txt','rt');
hdr = fgetl(fid);
tmp = textscan(fid,'%s%f%f%s%f%s',opt{:});
fclose(fid);
hdr = regexp(hdr,'[^,]+','match');
out = cell2struct(tmp,hdr,2)
out = struct with fields:
TIME: {3×1 cell} LAT: [3×1 double] LON: [3×1 double] STATUS: {3×1 cell} ALT: [3×1 double] RATE: {3×1 cell}
out.TIME
ans = 3×1 cell array
{'06:02:00'} {'06:02:01'} {'06:02:02'}
out.LAT
ans = 3×1
30.0010 30.0020 30.0040
out.LON
ans = 3×1
31.0020 31.0030 31.0040

댓글 수: 1

Walter Roberson
Walter Roberson 2021년 12월 18일
편집: Walter Roberson 2021년 12월 18일
after getting hdr, pass it through genvarname(), which will convert invalid names (such as containing spaces or punctuation) into valid names, and will further modify names to ensure that they are unique (in case two columns has the same name, one will be given a suffix)

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

카테고리

제품

릴리스

R14SP2

질문:

2021년 12월 18일

편집:

2021년 12월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by