sequential files(write, read ,append)?
2 次查看(过去 30 天)
显示 更早的评论
I'm having trouble understanding why my program wont work. I'm given an array of numbers. This is what i should be getting
45 66 78 23 41 55 88 66 99 24 74 88 -these should be in a vertical column
The number of lines of the integers is 12.
The max number is 99 in line 9.
The min number is 23 in line 4.
Heres what i have:
fid1=fopen('hw9.txt','w');
for x=[45 66 78 23 41 55 88 66 99 24 74 88]
fprintf(fid1,'%g\n',x);
end
fclose(fid1);
fid1=fopen('hw9.txt','r');
[numbers count] = fscanf(fid1,'%d');
[value index]=max(x);
[value2 index2]=min(x);
fclose(fid1);
disp(numbers)
fprintf('The number of lines of intergers is %g.\n',count)
fprintf('The max number is %g in line %g.\n',value,count)
fprintf('The min number is %g in line %g.\n',value2,index2)
Is the max function counting most common numbers?
0 个评论
采纳的回答
Paulo Silva
2011-4-20
Your code had some mistakes, here's the fixed code
%create the file and write values to it
fid1=fopen('hw9.txt','w');
for x=[45 66 78 23 41 55 88 66 99 24 74 88]
fprintf(fid1,'%g\n',x);
end
%the for loop writes every value to the file, last one is 88, x=88
fclose(fid1);
%read the values inside the file
fid1=fopen('hw9.txt','r');
[numbers count] = fscanf(fid1,'%d');
[value index]=max(numbers); %replaced x by numbers, x value is 88
[value2 index2]=min(numbers); %same as above
fclose(fid1);
disp(numbers)
fprintf('The number of lines of intergers is %g.\n',count)
%in the next line you had count instead of index (strange mistake)
fprintf('The max number is %g in line %g.\n',value,index)
fprintf('The min number is %g in line %g.\n',value2,index2)
The max function finds the maximum value of the vector, the min finds the minimum value of the vector.
One extra feature that you might want is
fprintf('Unique Values and the times they appear\n')
disp([unique(numbers), histc(numbers,unique(numbers))])
2 个评论
Paulo Silva
2011-4-20
yes they are variables, choose the name that best describes what they are for and avoid using the same names as matlab functions.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!