export an array to a text file

13 views (last 30 days)
Anna Baumgärtner
Anna Baumgärtner on 14 Jan 2021
Commented: dpb on 14 Jan 2021
I am trying to export an array into a .csv file but it doesnt seem to work.
this is what I'm trying to export:
alleszusammen=["clicks" "Sequenzlänge" "static" "rotate" "walk" "both";
NaN 4 c1prozent a1prozent b1prozent d1prozent;
NaN 5 c2prozent a2prozent b2prozent d2prozent;
NaN 6 c3prozent a3prozent b3prozent d3prozent
"trials" NaN NaN NaN NaN NaN;
NaN 4 g1prozent e1prozent f1prozent h1prozent;
NaN 5 g2prozent e2prozent f2prozent h2prozent;
NaN 6 g3prozent e3prozent f3prozent h3prozent]
I tried
csvwrite('1_Prozent_korrekt.csv', alleszusammen)
but I keep getting this error message:
Error using csvwrite (line 47)
Undefined function 'real' for input arguments of type 'string'.
Error in Auswertungsskript_fuer_SPSS (line 88)
csvwrite('1_Prozent_korrekt.csv', alleszusammen)

Answers (1)

dpb
dpb on 14 Jan 2021
As documentation for csvwrite says, it is able to write numeric data arrays only; you're trrying to include header data inside the data array that doesn't work.
MATLAB will not support the array as you've written it above; the 5th line with a string variable in the first position can't be stored in a numeric array even if you hold the first line of strings in an array of its own; you would need a cell array to hold this as shown and then use writecell
It would seem a likely candidate to turn into a table excepting for that fifth row -- what's it's relationship to the rest; just an indicator that the following data are trial results while the above are the associated conditions, maybe? If so, it would seem to hold in two tables would be the alternate or to add as columns in one; it's hard to suggest a specific storage without better idea of what is represented.
But, the answer to the question asked is "You can't do that, use a cell array and writecell instead."
  2 Comments
Anna Baumgärtner
Anna Baumgärtner on 14 Jan 2021
Thank you!
I used a cell array, but matlab doesn't know 'writecell'...
I am getting this error message now:
Undefined function or variable 'writecell'.
And when I try the help command:
help writecell
writecell not found.
Use the Help browser search field to search the documentation, or
type "help help" for help command options, such as help for methods.
dpb
dpb on 14 Jan 2021
Ah, too bad. writecell was introduced in R2019a; any chance you can upgrade?
The workaround to turn into one or two tables and then writetable would get you back to R2013b
Failing in either of those you'll have to revert to low-level i/o and fopen/fprintf.
MATLAB simplly did not support mixed types of input/output very well prior to the very recent introduction of the new family of writexxx routines, unfortunately.

Sign in to comment.

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!