MATLAB Answers

Help on txt file

1 view (last 30 days)
I3JM
I3JM on 22 Aug 2019
Commented: Guillaume on 23 Aug 2019
Hello,
I am pretty new to Matlab, so excuse my bad understanding of its natural behaviour... :D
I want to create a txt file from my workspace, so I tried:
A = [38;70;102;134;166];
B = [10];
C =[10];
D = [0];
Variablen = {'A =', 'B =', 'Umdrehungen =', 'C =', 'D = '};
Variablen_wert = [A;
B;
C;
D;];
log(2,:) = str2cell(Variablen_wert);
log(1,:) = Variablen;
fid = fopen('temp.txt','wt');
fprintf(fid,'%-4s %d\n',log{:});
fclose(fid);
The obvious problem is, A is a matrix, while the other Variables dont have the same size. I tried to convert them into a string, but that doesnt work as well.
In the end it hopefully should look like this:
A = 38 70 102 134 166
B = 10
C = 10
D = 0
Maybe you can help me on this :) thank you very much

  3 Comments

Guillaume
Guillaume on 22 Aug 2019
For me, the obvious problem is that you don't seem to pay much attention to the shape of the matrices in your desired output, so it's not clear what should happen if one of the variable happens to be a 2D matrix.
You write the A column vector as if it were a row vector. Isn't it a problem?
I3JM
I3JM on 23 Aug 2019
Unfortunately, that doesnt work neither... Tried both
Guillaume
Guillaume on 23 Aug 2019
Unfortunately, that doesnt work neither... Tried both
I'm not sure who this is addressed to. I wasn't offering a solution ... yet, since as I've said the desired text output is not consistent with the input (printing a row vector, when the input is a column). Until you've clarified if the inconsensistency is on purpose, I'm not going to offer a solution that may not do what's needed.

Sign in to comment.

Accepted Answer

AdamG2013468
AdamG2013468 on 22 Aug 2019
Just make one combined cell, then use the function writecell().
Variablen_wert = {'A =' [38 70 102 134 166];'B =' [10];'C =' [10]};
writecell(Variablen_wert, 'temp.txt');

  2 Comments

AdamG2013468
AdamG2013468 on 22 Aug 2019
you can modify the second line,
writecell(Variablen_wert, 'temp.txt', Delimiter, ' ')
If you don't want to print the comma delimiters.
I3JM
I3JM on 23 Aug 2019
Thanks for your quick answer. Huge problem: the Matlab Version i am currently working on is R2016b.. writecell is introduced in R2019a.
You know another way to achieve this?

Sign in to comment.

More Answers (0)

Tags