Grouping imported data and exporting into a .txt file

조회 수: 1 (최근 30일)
Jia Qing Soo
Jia Qing Soo 2013년 10월 3일
댓글: Cedric 2013년 10월 3일
I am suppose to import the following data from a .txt and then sort them accordingly. Those in bold are the variables, depending on the imported data. The sorting is done (>=80) - distinction, (>=50, <=79) - pass, (<=49) - file.
*56
88
32
16
99
78
44
63*
-----------------------------------------------------------------
The sample output to a .txt file is,
2 students scored a distinction. Their scores are
*88
99*
3 students scored a pass. Their scores are
*56
78
63*
3 students scored a fail. Their scores are
*32
16
44*
  댓글 수: 2
Cedric
Cedric 2013년 10월 3일
And what have you done or tried by yourself so far?
Jia Qing Soo
Jia Qing Soo 2013년 10월 3일
Thus far i have,
clear;clc;
fid = fopen('Input.txt');
a = fscanf(fid,'%g',inf);
fclose(fid);
len = length(a);
fid2 = fopen('Output.txt','wt');
I was thinking of using "if,else" but have no idea how to continue the code.

댓글을 달려면 로그인하십시오.

채택된 답변

Cedric
Cedric 2013년 10월 3일
편집: Cedric 2013년 10월 3일
Ok, the code that you provided in your comment is a good start. Here is a hint: define
>> x = [9, 3, 6, 7, 1, 2] ;
Then, for example
>> cond = x > 5
cond =
1 0 1 1 0 0
is a vector of logicals whose elements indicate where the condition is true (1) or false (0) for each element of x. A remarkable property of these vectors of logicals is that you can use them for indexing the original vector x, i.e. for getting all elements which satisfy the condition:
>> y = x(cond)
y =
9 6 7
Now you can work with y, e.g. compute its length, output it to file, etc..
Once you'll understand that, you'll have all you need to go on with this homework I guess.
  댓글 수: 2
Jia Qing Soo
Jia Qing Soo 2013년 10월 3일
편집: Jia Qing Soo 2013년 10월 3일
However, i am still having problem with the fprintf portion.
cond = a>= 80
y = a(cond)
fprintf('%g students scored a distinction. Their scores are\n%g',___,y)
The "___" is the portion i have no idea what to type. How do i calculate the length of variables that has >= 80? And my "y" has 2 numbers, so how do i display both numbers?
Expected output is,
2 students scored a distinction. Their scores are
*88
99*
Cedric
Cedric 2013년 10월 3일
For the first question:
doc length
For the second question, you cannot output an array with only one %g at the end of this formatSpec string that you are using. You have essentially two options: the first is a FOR loop over elements of vector y, which prints '%g\n' for each element. The second is a print using the same format '%g\n' directly on the y array, and use the fact that FPRINTF repeats the format as long as it "finds elements to output in the array".
I cannot say much more without giving you the answer directly. The best thing that you can do is to experiment with a small y array that you build by hand, and see whether you can understand what I said about repeated format, or try to implement a FOR loop.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by