How to save fprint as a string in .mat file
Show older comments
Hello everyone, I hope you are doing well. I have the following data and code which do clustering.
I want to save the result of the following which print cluster mean and max into string which store in .mat file
Like the ouput from
fprintf(formatSpec,i,Mcluster1PW) ,
fprintf(formatSpec,i,MclusterTimeseries),
fprintf(formatSpec,i,MclusterFrequncy)
fprintf(formatSpec,i,MclusterAmplitude)
will be save as string in .mat file which load and use for display
eva = evalclusters(dataset,'kmeans','silhouette','KList',[1:10]);
K=eva.OptimalK;
% K=2;
%Apply Kmeans to the dataset with Optimal Clusters K
disp('Calculating Centroid')
[idx,C,sumdist] = kmeans(dataset,K,'Display','final','Replicates',5);
Timeseriesdata=sort(dataset(:,1));
dataset_idx=zeros(size(dataset,1));
dataset_idx=dataset(:,:);
dataset_idx(:,5)=idx;
clusters = cell(K,1);
for i = 1:K
clusters{i} = dataset_idx(dataset_idx(:,5) == i,:);
end
for i = 1:K
T = clusters{i}(:,1);
fprintf('\nCLUSTER %d:\n',i)
DeltaT = diff(T);
MclusterTimeseries = mean(DeltaT);
formatSpec = 'Mean DeltaT of Cluster %d is %4e\n';
fprintf(formatSpec,i,MclusterTimeseries)
MclusterFrequncy = mean(clusters{i}(:,2));
formatSpec = 'Mean Frequncy of Cluster %d is %4e\n';
fprintf(formatSpec,i,MclusterFrequncy)
MclusterAmplitude = max(clusters{i}(:,3));
formatSpec = 'Max Amplitude of Cluster %d is %4.4f\n';
fprintf(formatSpec,i,MclusterAmplitude)
Mcluster1PW = mean(clusters{i}(:,4));
formatSpec = 'Mean Pulse Width of Cluster %d is %4e\n';
fprintf(formatSpec,i,Mcluster1PW)
end
Answers (1)
Rik
on 13 Jun 2022
0 votes
If you want to store the text output you should use sprintf to create the formated text. Then you can use fprintf to print to the command window.
Then you have the choice to store the char vectors (i.e. the text) as variables in a mat file, or use fopen and fprintf (and fclose) to write the text to a text file.
10 Comments
Stephen john
on 13 Jun 2022
Rik
on 13 Jun 2022
Which 'that' exactly? Replacing fprintf with sprintf doesn't sound like it should be the problem. So do you want to write to a text file or to a mat file?
Walter Roberson
on 13 Jun 2022
Edited: Walter Roberson
on 13 Jun 2022
for i = 1:K
T = clusters{i}(:,1);
OUT{1,i} = sprintf('\nCLUSTER %d:\n',i);
DeltaT = diff(T);
MclusterTimeseries = mean(DeltaT);
formatSpec = 'Mean DeltaT of Cluster %d is %4e\n';
OUT{2,i}= sprintf(formatSpec,i,MclusterTimeseries);
MclusterFrequncy = mean(clusters{i}(:,2));
formatSpec = 'Mean Frequncy of Cluster %d is %4e\n';
OUT{3,i} = sprintf(formatSpec,i,MclusterFrequncy);
MclusterAmplitude = max(clusters{i}(:,3));
formatSpec = 'Max Amplitude of Cluster %d is %4.4f\n';
OUT{4,i} = sprintf(formatSpec,i,MclusterAmplitude);
Mcluster1PW = mean(clusters{i}(:,4));
formatSpec = 'Mean Pulse Width of Cluster %d is %4e\n';
OUT{5,i} = sprintf(formatSpec,i,Mcluster1PW);
end
OUT = OUT(:);
OutfileName = 'OutputFile.txt';
[fid, msg] = fopen(OutfileName, 'w');
if fid < 0
error('Failed to open file "%s" because "%s"', OutfileName, msg);
end
fprintf(fid, '%s\n', OUT{:});
fclose(fid);
OutMatName = 'OutputFile.mat';
save(OutMatName, OUT);
Stephen john
on 14 Jun 2022
Stephen23
on 14 Jun 2022
save(OutMatName, 'OUT')
% ^ ^
Stephen john
on 14 Jun 2022
Stephen23
on 14 Jun 2022
"can we save it as string array?"
OUT = string(OUT);
save(OutMatName, 'OUT')
Walter Roberson
on 14 Jun 2022
OUT = string(OUT) ;
Stephen john
on 14 Jun 2022
Walter Roberson
on 14 Jun 2022
Hmmm...
Please show
size(OUT)
class(OUT)
OUT(31)
size(OUT(31))
class(OUT(31))
Categories
Find more on Data Type Conversion 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!