MATLAB Answers

How do I ask the user for a persons name and output the birthday of the person?

9 ビュー (過去 30 日間)
Nathan Kidd
Nathan Kidd 2018 年 4 月 6 日
回答済み: Elias Gule 2018 年 4 月 6 日
I am working on an assignment where I am given a .csv file with names in column one, birthmonth in column 3, and birthday (day of month) in column 4. I have to do a lot of things but what I am having trouble doing is making a system that allows the user to input a name and receive the birthday.
Eventually I put the birthday in a format that is simply days into the year (jan 5 = 5, Dec 31 = 365) and compare that to today's date to see how many days have passed/have yet to come since/until the persons birthday.
to put it simply, the user must: input a name, receive birthday (in the weird format).
Here is what I have so far, but I can't get it to work and I don't even think I'm on the right track:
input_file='employees.csv'; % in these lines I attempt to read the .csv
file=fopen(input_file); % into the program
data=textscan(file,'%s%s%d%d%f','delimiter',','); % I use a , delimiter
fclose(file); % close the file because that's just what you do
num_rows=size(data{1}); % This finds the number of rows in the .csv
employee.first_name=data{1}; % this creates a structure full of first names?
for i=1:num_rows % This prints out the whole .csv file
fprintf('%s %s, ',data{1}{i},data{2}{i});
fprintf('%d/%d %.2f\n',data{3}(i),data{4}(i),data{5}(i));
%this outputs:
%Donald Trump, 6/14 400000.00
%Mickey Mouse, 11/18 10000.00
%Ada Lovelace, 12/10 5000.00
%Justin Bieber, 3/1 125000.00
%Benjamin Franklin, 1/17 15000.00
%Angela Merkel, 7/17 234400.00
%columns 1 through 5 are: firstname, lastname, birthmonth, birthday, income
today=clock %just assigning some values for some obvious variables
year=today(1) % year = what year it is etc
%this gives me the amount of days its beensince the start of the year
d_in_m=[31,28,31,30,31,30,31,31,30,31,30,31] %this is a matrix of the days in each month
employee.first_name=data{1}; %assigning some values to two structures
birth_month=data{3}; %assigning some values to two cell arrays (I think)
%what I'm trying to do here is create an array called "index" that holds
%the birthday in the format of days into the year (jan5=5, Dec31=365) but I
%dont know if this would even help me if it worked.
for i=1:num_rows
for j=1:birth_month(i)-1
%this outputs a lot of similar numbers but the birthdays in the files are
%far away from each other so this shouldn't be the case.
%index =
%6×1 int32 column vector
%198 if true
% code

Community Treasure Hunt

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

Start Hunting!

Translated by