0

I am new to Matlab.

I want to save a mat to table files. I have a mat file like this. There is 1* 55 struct, which was stored in “data” field. the data structure of the table

I want to save the “track” field in these structs. And my code:

data = load('G:/my_file.mat');
my_output_file = 'G:/my_output.txt'

for my_track_data = data.data.track
    writetable(my_track_data, my_output_file)
end

But there is a error:

Expected one output from a curly brace or dot indexing expression, but there 55 results.

by the way, I can see the Matlab displayed all the results when I type: ''' data.data.track '''

All the 55 tables in the "data.data.track"

I tried just save it without a loop:

my_track_data = data.data.track
writetable(my_track_data, my_output_file)

It only save just the first table.

Chen Xing
  • 7
  • 3
  • `data.data` contains 55 structs. Each of the 55 structs contains a field `track`. What result do you expect of `data.data.track`? Which of the 55 fields do you want to access? You have to choose one. – Thomas Sablik Feb 20 '21 at 12:20
  • I want save 'track' field in each 55 structs to different files with format of txt – Chen Xing Feb 20 '21 at 12:22
  • You didn't answer the most important question for this error message: What result do you expect of `data.data.track`? There is no `track` in `data.data`. There are 55 structs in `data.data`. – Thomas Sablik Feb 20 '21 at 12:23
  • I expect it is a list with 55 tables. I can see Matlab display data.data.track. But it return my just one table – Chen Xing Feb 20 '21 at 12:25
  • If you expect `data.data.track` to contain 55 tables I don't understand the question. I thought `data.data` contains 55 structs. Why should `data.data.track` also contain 55 structs? It would help if you provide a [mcve]. – Thomas Sablik Feb 20 '21 at 12:28
  • Please don't post images of text and please add a [mcve] containing a minimal input file as text, not as link. Currently the structure is unclear for me. – Thomas Sablik Feb 20 '21 at 13:45
  • What is a complex mat? Google thinks of [this](https://www.google.com/search?tbm=isch&ei=PSUxYIuZOM3dtAaZmb_wBw&q=complex+mat&oq=complex+mat&gs_lcp=ChJtb2JpbGUtZ3dzLXdpei1pbWcQAzICCAAyAggAMgIIADICCAAyAggAOgIIKToICAAQsQMQgwE6BQgAELEDUOAcWNguYI4yaABwAHgAgAGJAYgB3wWSAQQxMC4xmAEAoAEBsAEB) and that’s also my first thought. But it makes no sense in your question... – Cris Luengo Feb 20 '21 at 15:06
  • Never mind. I followed this thread to "https://stackoverflow.com/questions/48970785/complex-matlab-struct-mat-file-read-by-python" to describe it. I tried to use to Python package "Scipy.io" to parse. And it showed me "matlabopaque" object. – Chen Xing Feb 20 '21 at 15:18

2 Answers2

0

Matlab loops do not work as Python loops (i.e. they do not request an iterator from the structure). Therefore, you should use traditional indexing:

for i = 1:numel(data.data)
    my_data = data.data(i);
    writetable(my_data.track, my_output_file)
end

(In fact, you can use the "iterator style", but the for iterates over columns, not rows, so the results end up being dependent on the shape of your data and will most likely not be what you want).

nonDucor
  • 2,057
  • 12
  • 17
-1
data = load('G:/my_file.mat');

# the braces is very important. It can convert 'data.data.track' to cell type for looping
required_data={data.data.track};

for k = 1:length(required_data)
    my_output_file = ['G:/test_' num2str(k) '.txt'];
    temp = requireddata(k);
    tt = cell2table(temp);
    writetable(tt.temp{1, 1}, my_output_file);
end


Chen Xing
  • 7
  • 3