Sum of a function into a new file

2 ビュー (過去 30 日間)
Claire Crowe
Claire Crowe 2020 年 10 月 21 日
回答済み: Srivardhan Gadila 2020 年 10 月 27 日
Please help me with this assignment!!
"Create a file called data1 by using the Matlab editor. For instance,
23
56
18
6
7
43
Write a Matlab script that reads data1, calculates the sum of its records sum1, and writes sum1 to the file called result."
Here is what I have so far, I am stuck on finding the sum and writing the sum into a file.
data1.m:
clc,clear
x = [23 56 18 6 7 43];
fid = fopen('rabFile.txt','w');
fprintf(fid,'%5.2g\n',x);
fclose(fid);
assignment_19.m:
fid = fopen('rabFile.txt','r');
if fid == -2
disp('Error opening file');
else
arr = fscanf(fid,'%g',[1 Inf]);
end
fclose(fid);
rabFile.txt:
23
56
18
6
7
43
  4 件のコメント
Walter Roberson
Walter Roberson 2020 年 10 月 21 日
you should be testing fid less than 0 rather than equal -2 by the way.
Rik
Rik 2020 年 10 月 22 日
You are writing data to a file in that first part of your code, so you know how to write data to a file.
If you search on Google for 'calculate sum Matlab' I expect the function you need will be the top result.

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

回答 (1 件)

Srivardhan Gadila
Srivardhan Gadila 2020 年 10 月 27 日
In addition to the suggestions in the comments, you can refer to the documentation of sum function.

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by