i am taking data from a txt file and doing some calculations on it and thn saving results into another. txt file but when i m opening it it is showing garbage value?
2 views (last 30 days)
Show older comments
clc
clear all
data1 = importdata('simran1.txt');
v =length(data1);
count=0;
disp('--------------before max term----------')
for i = 1:1000:v
fprintf('%f\n',data1(i));
count=count+1;
end
disp(count);
%for again printing max min and after 1000
disp('after min and max terms');
for i=1:1000:v
h=max(data1);
k=min(data1);
end
disp(k);
disp(h);
count1=0;
for i = 1:1000:v
fprintf('%f\n',data1(i));
count1=count1+1;
save('simran.txt','count','count1','data1','h','i','k','v');
end
disp(count1);
0 Comments
Answers (1)
dpb
on 26 May 2017
Edited: dpb
on 26 May 2017
Just using a filename extension of ".txt" doesn't change the default behavior of save which is binary .MAT-file format. Hence, if you open that file in a text editor it will look like garbage.
Use
save('simran.txt','count','count1','data1','h','i','k','v','-ascii');
See
doc save % for all the details of options and 'See Also' for alternates
NB: Unlike most languages, Matlab is "vectorized", meaning the reference above to a variable that is an array like data1 means the entire array. Hence, above where you have the save command in a for loop, you're going to write the ENTIRE data1 array every time thru that loop. If it is really multiple 1000's long, that's going to be a very, very big ASCII file.
Not at all sure what you're trying to do here or if it's just learning exercise, but also
for i=1:1000:v
h=max(data1);
k=min(data1);
end
is doing the same operation and will return the same result every pass--the loop has no bearing on what max|min are operating over; the full array is passed every time so the output is also the same. The loop serves no purpose at all here...
0 Comments
See Also
Categories
Find more on Whos 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!