MATLAB Answers

0

Problem appending a variable to an existing text file using save

Asked by Matteo Niccoli on 26 Mar 2015
Latest activity Edited by James Tursa
on 26 Mar 2015
I have some measurements in a cell array.
data ={...
'AS-01_09' [ 52] [451] [4724] [1]
'AS-01_08' [38.3000] [451] [4724] [1]
'AS-01_07' [ 37] [451] [4724] [1]
'AS-01_06' [23.1000] [451] [4724] [1]
'AS-01_05' [25.6000] [451] [4724] [1]
'AS-01_04' [ 89] [451] [4724] [1]
'AS-01_03' [75.8000] [451] [4724] [1]
'AS-01_02' [49.6000] [451] [4724] [1]
'AS-01_01' [26.3000] [451] [4724] [1]
'AS-02_5' [38.2000] [489] [4736] [2]
'AS-02_4' [ 45] [489] [4736] [2]
'AS-02_3' [44.7000] [489] [4736] [2]
'AS-02_2' [44.2000] [489] [4736] [2]
'AS-02_1' [47.3000] [489] [4736] [2]};
Column 1 has station codes (names), column 2 measurement values, column 3 local X coordinates, column 4 local Y coordinates, column 5 location flag (there are multiple stations for each location).
I also have in a separate cell location names. Stations from the same location have the same location name.
loctns={...
'AS-01'
'AS-01'
'AS-01'
'AS-01'
'AS-01'
'AS-01'
'AS-01'
'AS-01'
'AS-01'
'AS-02'
'AS-02'
'AS-02'
'AS-02'
'AS-02'};
I am trying to separate the measurements by location and save each location to a separate file. I also want to smooth the measurements and do the same for the smoothed measurements.
This is my code:
%%Get measurements' matrix
measurements=cell2mat(data(:,2:5));
%%Split 'measurements' matrix based on flag in first column
A = arrayfun(@(x) measurements(measurements(:,4) == x, :),...
unique(measurements(:,4)),'uniformoutput', false);
%%Get the list of unique location names
loctn_names=unique(loctns);
%%Smooth measurements
% Save smoothed and original values to a separate file for each location
[rw,cw]=size(loctn_names);
count=0;
for i = 1:rw
values = A{i,:}(:,1); % access arrays in 'A' with content indexing (curly
% brackets) then access first column (values) with
% regular indexing (parentheses)
j=length(values); % access variable 'data' to create
stations=data(count+1:count+j,1); % a 1D array of station names
count=count+j; % to append to the output file
IN = A{i,1}(:,1:3); % creating matrix of data for each loctn
tempname=strcat(char(loctn_names(i)),'_X-Y-V.txt'); % temporary file name
save(tempname, 'IN', '-ASCII'); % save matrix of data to output file
%save(tempname, 'stations', '-append'); % append station names to file
valuesSM=conv(values,[0.5 1 0.5],'same')/2; % smoothing by convolution
valuesSM(1)=values(1);
valuesSM(end)=values(end); % ensuring extremes are same
% as in original values
OUT = IN; OUT(:,1)=valuesSM(:); % creating matrix of data for each loctn
tempname1=strcat(char(loctn_names(i)),'_X-Y-smooth_V.txt') % file name
save(tempname1, 'OUT', '-ASCII'); % save matrix of data to output file
%save(tempname1, 'stations', '-append'); % append stations names to file
end
It is all working as expected, except for the two lines to append the station names, which are commented out. If I uncomment them, I get this error message:
Error using save
Unable to write to MAT-file \\...\Measurements\AS-01_X-Y-V.txt
File may be corrupt.
Does the append option as described in here only apply to .mat files? If yes, in what other way can I append the variable to the output file?
Thank you.

  0 Comments

Sign in to comment.

1 Answer

Answer by James Tursa
on 26 Mar 2015
 Accepted Answer

You still need to tell MATLAB that it is an ASCII text file you are writing to and not a mat file. E.g.,
save(tempname1, 'stations', '-append', '-ascii')

  2 Comments

HI James, thanks for your answer. if I do as you suggest, I do not get the error, but I do get a warning:
Warning: Attempt to write an unsupported data type to an ASCII file.
Variable 'stations' not written to file.
So I still do not get the desired result. Is it not possible to append strings? Any suggestions about this?
Yes, it appears save will convert your string to numeric and then write that out. You may have to resort to fwrite for this. E.g.,
fid = fopen(tempname1,'at'); open in append ASCII text mode
fwrite(fid,stations,'*uchar'); % append the string as character data
fprintf(fid,'\n'); % append a newline
fclose(fid); % close the file
Or you may need to wrap a loop around the fwrite and fprintf if stations is a cell array of strings and you want to print one string per line, etc.

Sign in to comment.