Help with function dealing with struct and importing data.
Mostrar comentarios más antiguos
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. ‘specs’ is indeed a struct.
2. The four fields above all exist.
3. The fields ‘file_dir’, Tile_name’ are (non-empty) strings
4. The fields ‘nimes’, ‘my_line’ are positive integers.
c) Now suppose you have a directory whose name is stored in
‘specs.file_dir’ which 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.nlines’ number 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_line’ specifies 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
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Structures en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!