Writing code for function

2 views (last 30 days)
Lauren Kinchla
Lauren Kinchla on 17 Nov 2019
Answered: Image Analyst on 17 Nov 2019
If one input is a character array in CSV, I need to write a function that will return 1 if an input of a character array is contained in the first input, and 0 if it is not.

Answers (1)

Image Analyst
Image Analyst on 17 Nov 2019
Not sure what you want, and how this has to do with a CSV file, but there is already a built-in function "that will return 1 if an input of a character array" -- it is called ischar(). If you want, you can use fgetl() to call ischar() on every single line you read it.
% Open the file.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
if ischar(textLine(1))
% If first character of textLine is a character, do something.
end
% Read the next line.
textLine = fgetl(fileID);
end
% All done reading all lines, so close the file.
fclose(fileID);

Tags

Community Treasure Hunt

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

Start Hunting!