Filter löschen
Filter löschen

Grouping imported data and exporting into a .txt file

1 Ansicht (letzte 30 Tage)
Jia Qing Soo
Jia Qing Soo am 3 Okt. 2013
Kommentiert: Cedric am 3 Okt. 2013
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 Kommentare
Cedric
Cedric am 3 Okt. 2013
And what have you done or tried by yourself so far?
Jia Qing Soo
Jia Qing Soo am 3 Okt. 2013
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Cedric
Cedric am 3 Okt. 2013
Bearbeitet: Cedric am 3 Okt. 2013
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 Kommentare
Jia Qing Soo
Jia Qing Soo am 3 Okt. 2013
Bearbeitet: Jia Qing Soo am 3 Okt. 2013
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 am 3 Okt. 2013
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by