I am trying to get peoples names from a csv file using their birth month, how can I do this?
2 ビュー (過去 30 日間)
古いコメントを表示
Hi everyone I just went through a class from matlab and C programing last semester, so I'm pretty new to programming. I am trying to get a code working that will prompt the user to input a month then the program will check a file and pull the names from that and print them to the screen. This is my code so far:
%Finding people who have a birthday this month
[num, char, raw] = xlsread('book1.csv');
%Search month
monthcheck = input('Enter name of month to search:', 's');
%Reading file
[r, c] = size(raw);
x = 1;
y = 0;
for i = 1:r
%The structure will be set as month, day, and lastname
employees(i).monthcheck = raw(i,3);
for a = 2
employees(i).firstname(a-1) = char(i,1);
end
for b = 2
employees(i).day(b-1) = raw(i,4);
end
for c = 2
employees(i).lastname(c-1) = raw(i, 2);
end
end
repQP=0;
%Allow the code to search for months that are duplicated in the Book 1 data,
%and determining whether the month actually exists or not in the book 1 data
for i = 1:r
%if both of the strings are read by the code as the same, this mean
%repition is occuring which would be 1
repititionQP = strcmp(monthcheck, employees(i).monthcheck);
if(repititionQP == 1)
repQP = repQP + 1;
end
%Else if statement will be utilize to determine the position of the
%name in the book 1 data set
if(repQP == 0)
x = x + 1;
firstname = char(x,1);
lastname = char (x,2);
end
end
%If the code cannot find the month in book 1 data, then it will display a
%statement letting user know that the name was not found.
%The code will also ask user to submit a last name, so the code can
%reattempt to find the name again.
if(repQP == 0)
disp('ERROR! The month was not found');
end
%Displaying all first and last names with that birthday month
fprintf ('People who have birthdays in %s are:\n %s %s\n', monthcheck, firstname, lastname);
These are the errors I'm getting:
Error using fprintf Function is not defined for 'cell' inputs. Error in Birthdaycheck (line 55) fprintf ('People who have birthdays in %s are:\n %s %s\n', monthcheck, firstname, lastname);
Also here are a few lines of my csv with their layout:
Barry French March 23
Carlos Soltero September 2
Monica Federle January 31
Dorothy Smith July 14
Salvador Daly March 29
Claudia Schneider December 25
I pulled some of this code from a program I did in class to find the days from/till a birthday in a csv file. Any help would be appreciated and thanks in advance for any help! Also Sorry this was so long.
0 件のコメント
回答 (1 件)
Walter Roberson
2018 年 4 月 12 日
raw is a cell array, so
employees(i).monthcheck = raw(i,3);
is copying a cell array into employees(i).monthcheck rather than the contents of the cell, which would be raw{i,3};
Note:
x = x + 1;
firstname = char(x,1);
Your x is numeric. char(x,1) means the same as [char(x), char(1)] . That does not format the numeric value of x as a printable decimal number: that looks up the character code associated with unicode position #x . For example when x = 1, then char(x) is the Start of Header control character, https://www.fileformat.info/info/unicode/char/0001/index.htm
Shouldn't you be copying something from either raw or the employees structure ?
8 件のコメント
Walter Roberson
2018 年 4 月 13 日
編集済み: Walter Roberson
2018 年 4 月 13 日
The code would replace pretty much everything after you had read in the file and store it into your employees structure.
The variable mask would be a logical vector, and the code is jus using logical indexing. See https://www.mathworks.com/help/matlab/math/matrix-indexing.html#bq7egb6-1
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!