Input/Output Function's, need content and display help

18 ビュー (過去 30 日間)
Kyle
Kyle 2011 年 4 月 21 日
コメント済み: Walter Roberson 2023 年 11 月 20 日
Im creating a function to output future value of invested money by F=P*(1+(i/1200))^n, where i us interest rate, P is initial invested money, and n is the number of months. I need to input P,i,and n, which i have done. I need to get both the months and future values to display in side by side columns like this:
Month Future Value
===== ============
1 $502.08
2 $504.18
and so on..
Here's what I've been able to come up with:
fid=fopen('interest.txt','w');
P=input('Enter initial investment: ');
i=input('Enter investment interest rate(in %): ');
x=input('Enter investment period(in months): ');
fprintf('Output file sucessfully created.')
for n=1:x
F=P*(1+(i/1200))^n
end
fclose(fid)
fid=fopen('interest.txt','r');
[n F]=fscanf(fid,'%d');
fclose(fid);
disp(n)
disp(F)
I don't know why the months and Future values aren't displaying, but the Future value from the for loop does. but more importantly, how should i be displaying this. I'm assuming fprintf but i cant get the F and N to display.
  1 件のコメント
Walter Roberson
Walter Roberson 2011 年 4 月 21 日
Thank you for showing your code (and for taking the time to format it) !

サインインしてコメントする。

採用された回答

Matt Fig
Matt Fig 2011 年 4 月 21 日
Here is how I would do it, if I was going to write to the file then display the file contents in two steps...
fid = fopen('interest.txt','wt');
P = input('Enter initial investment: ');
I = input('Enter investment interest rate(in %): ');
x = input('Enter investment period(in months): ');
for n=1:x
F=P*(1+(I/1200))^n;
fprintf(fid, '%d\t$%.2f\n', n, F);
end
tf2 = fclose(fid);
if fid~=-1 && ~tf2
fprintf('\t\t\nOutput file successfully created.\n\n');
else
fprintf('\t\t\nProblem encountered opening or closing file.\n\n');
end
fid = fopen('interest.txt','r');
T = textscan(fid,'%d\t$%.2f');
n = T{1};
F = T{2};
fprintf('Month\tFuture Value\n\n')
for ii = 1:length(n)
fprintf('%d\t\t$%.2f\n',n(ii),F(ii))
end
fclose(fid);

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2011 年 4 月 21 日
In the loop:
fprintf(fid, '%d %.2f\n', n, F);
I also recommend that you re-read the documentation on the two-output form of fscanf
  4 件のコメント
Kyle
Kyle 2011 年 4 月 21 日
Is the writing to file part correct with your fprintf statement or does something else need to be done. I've looked over my book, which doesn't really explain this well. I dont see anything obviously wrong fscanf statement, unless my the [n F] is wrong. fscanf(fid,format,size) is what my book says, but i don't see any reason of inputing a size because i've seen it done with out it. Would size=inf be necessary?
Walter Roberson
Walter Roberson 2011 年 4 月 21 日
That fprintf() should be enough to write the file.
The two-output form of fscanf() returns the _number_ of elements read in the second argument. The naming of your variables suggest that you are expecting that fscanf() will return one variable per column of inputs... it won't!

サインインしてコメントする。


SANTHOSH
SANTHOSH 2023 年 11 月 20 日
編集済み: Walter Roberson 2023 年 11 月 20 日
fid = fopen('interest.txt','wt');
P = input('Enter initial investment: ');
I = input('Enter investment interest rate(in %): ');
x = input('Enter investment period(in months): ');
for n=1:x
F=P*(1+(I/1200))^n;
fprintf(fid, '%d\t$%.2f\n', n, F);
end
tf2 = fclose(fid);
if fid~=-1 && ~tf2
fprintf('\t\t\nOutput file successfully created.\n\n');
else
fprintf('\t\t\nProblem encountered opening or closing file.\n\n');
end
fid = fopen('interest.txt','r');
T = textscan(fid,'%d\t$%.2f');
n = T{1};
F = T{2};
fprintf('Month\tFuture Value\n\n')
for ii = 1:length(n)
fprintf('%d\t\t$%.2f\n',n(ii),F(ii))
end
fclose(fid);
  1 件のコメント
Walter Roberson
Walter Roberson 2023 年 11 月 20 日
The only difference between this solution and what @Matt Fig posted over a decade ago in https://www.mathworks.com/matlabcentral/answers/5994-input-output-function-s-need-content-and-display-help#answer_8321 is in the spacing between lines.
What extra value do you feel that your post brings to the topic?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeBig Data Processing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by