Grouping imported data and exporting into a .txt file
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Cedric
on 3 Oct 2013
Edited: Cedric
on 3 Oct 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 Comments
Cedric
on 3 Oct 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.
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!