Help with function dealing with struct and importing data.

I'm not very familiar with struct as of yet and am not sure if I am calling the different fields in it correctly or reading the files into a cell array correctly.
This is the prompt for this function:
a) Write a function named ‘hw4a’ which has one input parameter
(specs) and one output parameter (‘ret_line’). Your function
should do the following:
b) Suppose ‘specs’ is a struct with 4 fields: {file_dir, file_name, nimes,
my_line}. Use if-end or if-elseif-end statements to check that:
1. specsis indeed a struct.
2. The four fields above all exist.
3. The fields ‘file_dir’, Tile_nameare (non-empty) strings
4. The fields ‘nimes’, my_lineare positive integers.
c) Now suppose you have a directory whose name is stored in
specs.file_dirwhich contains a file whose name is stored in
specs.file_name. You should assume that you have a simple text
file which contains text (NOT numeric data).
d) Your function should open the file (hint: use fopen), and read
specs.nlinesnumber of lines (rows) from the file into a cell array
C. Each element of C should contain one line from your file. You
can assume that there are no errors in the parameter
specs.nlines, that is, it cannot exceed the number of lines in your
file.
e) Suppose, specs.my_linespecifies some line number. The output
parameter ‘ret_line’ should be set to this line (you should return a
string).
f) Verify that your code works by saving some text into a file
(created in notepad / textedit) and saving the file with extension
.txt. You should attach the file with your code.
Here is my code:
function [ ret_line ] = hw4a( specs )
fields=isfield(specs, {'file_dir', 'file_name', 'nlines', 'my_line'});
if isstruct(specs)==0
disp('specs is not a struct');
return
elseif sum(fields)~=4
disp('all fields do not exist');
return
elseif ischar(file_dir)==0
disp('file_dir is not a non-empty string');
return
elseif ischar(file_name)==0
disp('file_name is not a non-empty string');
return
elseif nlines<1
disp('nlines is not a positive integer')
return
elseif my_line<1
disp('my_line is not a positive integer')
return
end
fn=fopen('%s%s.txt', specs.file_dir,specs.file_name, 'r');
if fn==-1
disp('file did not open correctly')
return
end
S=(1:specs.nlines);
for M=1:specs.nlines
S(M) = fgetl(fn);
end
C=cellstr(S);
ret_line=C{specs.my_line};
fprintf(specsmy_line.txt,'%s',C{specs.my_line});
SOPEN = fclose(fn);
if SOPEN == -1;
disp('file did not close correctly')
end
end
The prompt mentions to attach the text file to the function. Does this mean I have to create the text file beforehand and then write into it? Another this I am worried about is calling the fields in the struct so please help me figure out if I did those correctly.
Thank you

 Accepted Answer

I think the instruction about attaching the text file just means that you submit both the m-file (containing your code) and the text file (containing the input for the program). You probably have to attach both of them to an email for submission. However, if there's any doubt, you should check with your instructor.
I haven't checked all your code, but one clear mistake is:
fn=fopen('%s%s.txt', specs.file_dir,specs.file_name, 'r');
and I wonder if what you mean is
fn=fopen(fprintf('%s%s.txt', specs.file_dir,specs.file_name), 'r');

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!