hey all!
i'm new to matlab and i have this task basically: i have a text file of students' their id's and grades (after that the text goes down a line) and i need to make an array/structure out of it.
the example text is stud_ex.txt. all i know is how to use fopen but beyond that i'm clueless.
i see a lot of people online using things like "%" signs and i have no idea what these mean.
if anyone can shed some light i would really appreciate it.

 採用された回答

Chris
Chris 2022 年 6 月 19 日
編集済み: Chris 2022 年 6 月 19 日

0 投票

fopen is for low-level file operations you may not need.
Perhaps try
data = importdata('stud_ex.txt')
Which will give you a struct with three fields:
data: [7×4 double]
textdata: {7×1 cell}
rowheaders: {7×1 cell}
The row headers are irrelevant, since you don't have headers. But you could use data.data and data.textdata.
Split the data array into student IDs and grades.

4 件のコメント

daniel slama
daniel slama 2022 年 6 月 20 日
it's crazy how many more efficient tools there are out there to help you that the univercity courses never teach you lol.
thank you.
Chris
Chris 2022 年 6 月 20 日
On the other hand, if the point of the lesson is to learn those things (which may be less efficient for you to program, but can run much faster if you know the exact format of the file and write a custom function for it), Mathworks has a nice summary here. fscanf, sscanf, etc, all have a format specification in common, which takes a bit of thinking to learn but can be quite powerful.
daniel slama
daniel slama 2022 年 6 月 20 日
omg that is JUST what i needed. ty!
Chris
Chris 2022 年 6 月 20 日
編集済み: Chris 2022 年 6 月 20 日
You're welcome.
There are also intermediate steps you could take:
fid = fopen('stud_ex.txt','r'); % Open file and attach a handle to it
thisline = fgetl(fid);
parsedline = split(firstline);
studentnames{1} = parsedline{1};
% ... do something like this in a "while ~feof(fid)" loop
fclose(fid) % Release the file
Convert the lines to strings for even easier handling.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Import and Export についてさらに検索

製品

リリース

R2020b

質問済み:

2022 年 6 月 19 日

編集済み:

2022 年 6 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by