0

I have a .mat file which contains a struct (called wiki),

enter image description here

in which there is a field called full_path containing data as follows:

ans = 
Columns 1 through 4

{'17/10000217_198…'}    {'48/10000548_192…'}    {'12/100012_1948-…'}    {'65/10001965_193…'}

Columns 5 through 8

{'16/10002116_197…'}    {'02/10002702_196…'}    {'41/10003541_193…'}    {'39/100039_1904-…'} 
and so on

How can I create a .csv file with the data present in the curly braces?

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
Anurag
  • 37
  • 1
  • 6
  • Just to make sure I understand: you want the columns of val to be written to a csv file as a single line, i.e. `17/10000217_198…,48/10000548_192…` etc. ? Also, what did you try? – Dev-iL Oct 07 '18 at 12:54
  • Previously I tried the same function, but I passed the text file instead. How can I pass this val ? – Anurag Oct 07 '18 at 16:26

1 Answers1

0

This is quite a common problem, which requires very basic functions:

wiki = struct2array(load('wiki.mat', 'wiki'));
fid = fopen('q52688399.csv', 'w');
fprintf(fid,'%s\n', wiki.full_path{:});
fclose(fid);

The above will produce a ~2MB text tile containing a single column of strings.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • As `{'16/10002116_197…'}` is a file name every character of it is occupying individual cell. How can I keep a filename limited in a single cell only? – Anurag Oct 07 '18 at 16:42
  • Did you try the code? The way I created `val` in my example is just a demonstration that shows that each cell is converted to a different "entry" in the resulting `.csv`. If your cells contain long strings - you'll get long strings. If your cells contain a single character each - you'll get a single character per entry. – Dev-iL Oct 07 '18 at 18:23
  • Actually, I have wiki.mat file, inside which there are multiple fields as gender,full_path,location etc. Now the above shown `val` data is inside full_path. So how do I write script for this? – Anurag Oct 10 '18 at 09:05
  • Start by loading the required field: `val = struct2array(load('wiki.mat','full_path'));` then `csvwrite('filename.csv', val);`... It should be as simple as that. – Dev-iL Oct 10 '18 at 09:22
  • Please see the screenshot [link](https://drive.google.com/open?id=1dwMDS4_G1yAukWd9WPTPMWoxwo8i2nXI). Also link to .mat file is [link](https://drive.google.com/open?id=1tDsPBlDekAGb06QpAKPZm7iR8qlhrq2P). Please suggest required commands. – Anurag Oct 10 '18 at 17:10
  • @Anurag Please see the updated answer. If it is sufficient, consider [accepting](https://meta.stackexchange.com/questions/5234) the answer. – Dev-iL Oct 11 '18 at 08:39