Text file spacing values so they start at the same point on each new line
16 views (last 30 days)
Show older comments
Emma Jacobs
on 3 Apr 2023
Answered: Walter Roberson
on 3 Apr 2023
How can I create a text file where the values in the file start at the same point for each line? Like this (basically line up the beginning of each value with the one above it):
3.695652174 -48.04347826 23.18181818 4 1 TEST1
-18.47826087 -11.08695652 30.90909091 4 1 TEST2
40.65217391 -77.60869565 0 4 1 TEST3
18.47826087 33.26086957 0 4 1 TEST4
This is what I have right now, and it is returning a text file with what I want, but the values are 'misaligned':
k=1;
output=strings(80,1);
l=size(EEG_band.dipfit.model,2)
nodefile = fopen('output.node', 'w');
for i = 1:l
% Check if the value in the third column is below the threshold
temp = EEG_band.dipfit.model(i).rv
if temp < 0.15
% If it is, add the values in the first and fourth columns to the
% output cell array and .node file
tempposxyz=EEG_band.dipfit.model(i).posxyz;
tempstring = sprintf('%0.4f %0.4f %0.4f 4 1 %s\n', tempposxyz(1), tempposxyz(2), tempposxyz(3), EEG_band.dipfit.model(i).areadk);
output(k,1) = tempstring;
fprintf(nodefile, tempstring);
k=k+1;
end
end
output=output(output~='');
% Write the values in the output cell array to a text file
fclose(nodefile)
The output from this is below:
3.6957 -48.0435 15.4545 4 1 superiorparietal R
-11.0870 -33.2609 23.1818 4 1 superiorparietal L
3.6957 -70.2174 15.4545 4 1 superiorparietal R
-3.6957 -33.2609 38.6364 4 1 superiorparietal L
25.8696 -77.6087 7.7273 4 1 lateraloccipital R
11.0870 -55.4348 7.7273 4 1 superiorparietal R
18.4783 -25.8696 46.3636 4 1 superiorparietal R
Any help is appreciated!
2 Comments
Walter Roberson
on 3 Apr 2023
What is the difference between this and https://www.mathworks.com/matlabcentral/answers/1939939-make-values-or-words-in-a-text-file-start-at-the-same-point-in-each-line?s_tid=srchtitle ?
Accepted Answer
Walter Roberson
on 3 Apr 2023
There are three strategies:
- when you are using numeric or text columns whose width differs by no more than 7, then use the tab character between the format elements. Tab is represented as \t in format strings
- when you have a guaranteed maximum width for a column (taking into account possible negative sign), use a fixed-width format item, such as %8.4f or %16s . Note that by default these right-align within the width -- leading spaces. If you want left alignment (trailing spaces) use a negative width, such as %-8.4f or %-16s
- if you need to use the minimum width between columns that is compatible with the values you happen to have (for example this particular data subset does not need more than 9.9999 but the next one might need a larger number), then start by formatting the minimum width storing the formatted values; I recommend using compose and pass a string() format rather than a character vector format. Then find the length of each formatted item, and take max() over each column. Use that per-column width and %s format with width specification and one space between format items, like '%12s %12s %18s' . Now use that format to format the columns; I recommend compose for this as well. The result will be one pre-formatted string per line, that you can then display or write to file.
0 Comments
More Answers (0)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!