Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Problems with Strings!

Subject: Problems with Strings!

From: Mary

Date: 25 Jun, 2013 19:16:06

Message: 1 of 5

Hello!

I'm writing a code for which a user inputs a line of strings, and matlab matches those strings to data in a loaded xls spreadsheet, runs calculations and then spits out the data into a new spreadsheet.

I'm having two problems:
1.) user input - I cant seem to get this to work properly. MATLAB insists that the user is loading in a string character by character and wont take the list of inputs as a vector of strings.

(I'm hoping to allow the user to copy and paste a list of usernames from a word doc/spreadsheet, and paste them when prompted for an input)

2.) generating the spreadsheets
for some reason MATLAB refuses to acknowledge my column headers when using xlswrite. I even followed a reccomended tutorial from MATLAB for how to set things up!

This is what I have so far!



function KRNS_statusReport
%% User Input Prompt
% Prompts the user to load the current module status .xls file
fprintf('Select the current module status log. File must be in .xls format')
[FileName , PathName ] = uigetfile('*.xls','Select the current module status log')
modStatus = fullfile(PathName,FileName);
[num, txt, raw]= xlsread(modStatus);


***PROBLEM 1. HERE***
x = strcat(input('Enter usernames for current cohort','s'));
userNames = strsplit(x)



a=1;
%% String Match Input Usernames with those in Module Status Spreadsheet
for i= 1:numel(userNames)
compare= strcmp (userNames(a),raw(:,1))
username = userNames(a)
usernameLocation = find(compare, 1)
moduleLocation = usernameLocation - 1;
completedModules = num(moduleLocation,3)
completedBlocks = num(moduleLocation,2)
startTime = raw(usernameLocation,5)
endTime = raw(usernameLocation,6)

totalCompleted = 0;
partialUnderFive = 0;
partialUnderTen = 0;
partialOverTen = 0;
noLogin = 0;

%% Identify which category their completion status falls in
    if completedModules == 0;
        totalCompleted = totalCompleted +1;
    elseif completedModules < 5;
        partialUnderFive = partialUnderFive +1;
    elseif 10 > completedModules >= 5
        partialUnderTen = partialUnderTen +1;
    elseif completedModules > 10
        partialOverTen = partialOverTen +1;
    else
        noLogin = noLogin+1;
    end
    
%% Calculate Participant Payments

if completedModules == 0;
    payment = 189.07
elseif completedModules ==13;
    payment = ((completedModules -1)*11.16) + (completedBlocks * (11.16/16))
else
    payment = ((completedModules -1) * 11.16)+ (completedBlocks *.36)
end


log(a,2) = mat2cell(completedModules);
log(a,3) = mat2cell(completedBlocks);
log(a,4) = mat2cell(payment);
log(a,1) = (username(a));

a=a+1;

end

%% Show Values and Generate Statistics Log
%Displays values in MATLAB Command Window

totalCompleted
partialUnderFive
partialUnderTen
partialOverTen
noLogin

*** PROBLEM 2 HERE***
%Stores Variable Data in a Matrix
colHeader = {'Number Full Completion','Number Partial Completion Less than Five','Number Partial Completion Less than Ten','Number Partial Completion More than Ten','Number No Login'};
stats(1) = (totalCompleted);
stats(2) = (partialUnderFive);
stats(3) = (partialUnderTen);
stats(4) = (partialOverTen);

%% Saves Data into an xls spreadsheet
saveName = ['Module_Status_Report' date]
xlswrite(saveName,log,1)
xlswrite(saveName,stats,2,'A2')
xlswrite(saveName,colHeader,2,'A1')
end

Subject: Problems with Strings!

From: dpb

Date: 25 Jun, 2013 20:09:40

Message: 2 of 5

On 6/25/2013 2:16 PM, Mary wrote:
...
> I'm having two problems:
...

> (I'm hoping to allow the user to copy and paste a list of usernames from
> a word doc/spreadsheet, and paste them when prompted for an input)
>
> 2.) generating the spreadsheets for some reason MATLAB refuses to
> acknowledge my column headers when using xlswrite. I even followed a
> reccomended tutorial from MATLAB for how to set things up!
>
> This is what I have so far!
>
>
...

> ***PROBLEM 1. HERE***
> x = strcat(input('Enter usernames for current cohort','s'));
> userNames = strsplit(x)
...

Play around w/ the following and see if any joy ensues...

s=inputdlg('input: ','Input Box',3);

If numlines>1 it appears will return a cell array of inputs from a
clipboard paste. If I open a Word doc and put, three text lines in it
and copy them I get

 >> s=inputdlg('input: ','Input Box',3)
s =
     [3x5 char]
 >> s{:}
ans =
Name1
Name2
Name3
 >>

If numlines==1 then it pastes them all on the one line.

doc inputdlg

...

> *** PROBLEM 2 HERE***
> %Stores Variable Data in a Matrix
> colHeader = {'Number Full Completion','Number Partial Completion Less
> than Five','Number Partial Completion Less than Ten','Number Partial
> Completion More than Ten','Number No Login'};
> stats(1) = (totalCompleted);
> stats(2) = (partialUnderFive);
> stats(3) = (partialUnderTen);
> stats(4) = (partialOverTen);
>
> %% Saves Data into an xls spreadsheet
> saveName = ['Module_Status_Report' date]
> xlswrite(saveName,log,1)
> xlswrite(saveName,stats,2,'A2')
> xlswrite(saveName,colHeader,2,'A1')
> end

I've never used xlswrite() so no help there, really, sorry...

I note the following on the "Data to write" expansion...

> to write, specified as a two-dimensional numeric or character
> array, or, if each cell contains a single element, a cell array.
>
> If A is a cell array containing something other than a scalar numeric
> or a string, then xlswrite silently leaves the corresponding cell in
> the spreadsheet empty.

Don't know if that's related to your problems/troubles/symptoms or not,
but is suggestive methinks...

--

Subject: Problems with Strings!

From: Mary

Date: 25 Jun, 2013 21:09:07

Message: 3 of 5

dpb <none@non.net> wrote in message <kqcte3$jk7$1@speranza.aioe.org>...
> On 6/25/2013 2:16 PM, Mary wrote:
> ...
> > I'm having two problems:
> ...
>
> > (I'm hoping to allow the user to copy and paste a list of usernames from
> > a word doc/spreadsheet, and paste them when prompted for an input)
> >
> > 2.) generating the spreadsheets for some reason MATLAB refuses to
> > acknowledge my column headers when using xlswrite. I even followed a
> > reccomended tutorial from MATLAB for how to set things up!
> >
> > This is what I have so far!
> >
> >
> ...
>
> > ***PROBLEM 1. HERE***
> > x = strcat(input('Enter usernames for current cohort','s'));
> > userNames = strsplit(x)
> ...
>
> Play around w/ the following and see if any joy ensues...
>
> s=inputdlg('input: ','Input Box',3);
>
> If numlines>1 it appears will return a cell array of inputs from a
> clipboard paste. If I open a Word doc and put, three text lines in it
> and copy them I get
>
> >> s=inputdlg('input: ','Input Box',3)
> s =
> [3x5 char]
> >> s{:}
> ans =
> Name1
> Name2
> Name3
> >>
>
> If numlines==1 then it pastes them all on the one line.
>
> doc inputdlg
>
> ...
>
> > *** PROBLEM 2 HERE***
> > %Stores Variable Data in a Matrix
> > colHeader = {'Number Full Completion','Number Partial Completion Less
> > than Five','Number Partial Completion Less than Ten','Number Partial
> > Completion More than Ten','Number No Login'};
> > stats(1) = (totalCompleted);
> > stats(2) = (partialUnderFive);
> > stats(3) = (partialUnderTen);
> > stats(4) = (partialOverTen);
> >
> > %% Saves Data into an xls spreadsheet
> > saveName = ['Module_Status_Report' date]
> > xlswrite(saveName,log,1)
> > xlswrite(saveName,stats,2,'A2')
> > xlswrite(saveName,colHeader,2,'A1')
> > end
>
> I've never used xlswrite() so no help there, really, sorry...
>
> I note the following on the "Data to write" expansion...
>
> > to write, specified as a two-dimensional numeric or character
> > array, or, if each cell contains a single element, a cell array.
> >
> > If A is a cell array containing something other than a scalar numeric
> > or a string, then xlswrite silently leaves the corresponding cell in
> > the spreadsheet empty.
>
> Don't know if that's related to your problems/troubles/symptoms or not,
> but is suggestive methinks...
>
> --



Thank you so much - I'll give these suggestions a try!

About the xlswrite it just seems crazy to me that it fills in the username information which is a string on sheet 1, but it wont fill strings in on sheet 2.

BTW - I feel like you've answered a billion of my questions on here, and I just wanted to say a sincere thank you. You have really made my journey of understanding MATLAB enjoyable :)

Subject: Problems with Strings!

From: dpb

Date: 25 Jun, 2013 22:29:41

Message: 4 of 5

On 6/25/2013 4:09 PM, Mary wrote:
> dpb <none@non.net> wrote in message <kqcte3$jk7$1@speranza.aioe.org>...
...

>> I've never used xlswrite() so no help there, really, sorry...
>>
>> I note the following on the "Data to write" expansion...
>>
>> > to write, specified as a two-dimensional numeric or character
>> > array, or, if each cell contains a single element, a cell array.
>> >
>> > If A is a cell array containing something other than a scalar numeric
>> > or a string, then xlswrite silently leaves the corresponding cell in
>> > the spreadsheet empty.
>>
>> Don't know if that's related to your problems/troubles/symptoms or
>> not, but is suggestive methinks...
...

> Thank you so much - I'll give these suggestions a try!
> About the xlswrite it just seems crazy to me that it fills in the
> username information which is a string on sheet 1, but it wont fill
> strings in on sheet 2.
> BTW - I feel like you've answered a billion of my questions on here, and
> I just wanted to say a sincere thank you. You have really made my
> journey of understanding MATLAB enjoyable :)

Not a problem...cs-sm is one of the newsgroups I follow to keep in hand
a little post-retirement from technical side (am now back on family farm)...

Anyway, I played w/ the xlswrite() function just a little -- looks to me
like it works for cell arrays. The following works here w/ 2012b and
old Excel...can't speak of new release.

 >> a={'Name1';'Name2';'Name3'}
a =
     'Name1'
     'Name2'
     'Name3'
 >> xlswrite('xlswr.xls',a{1},1,'A1') % a character per cell
 >> xlswrite('xlswr.xls',a(1),1,'A1') % string in A1
 >> xlswrite('xlswr.xls',a,1,'A1') % the three in A1:A3
 >> a'
ans =
     'Name1' 'Name2' 'Name3'
 >> xlswrite('xlswr.xls',a',1,'A1') % the three in A1:C1
 >>

Look at the example in

help xlswrite

it writes mixed data w/ the header row and array as one would expect/like...

Looks like w/o reading your code more thoroughly you've got a problem w/
the way you're trying to store/write the spreadsheet data.

--

--

Subject: Problems with Strings!

From: dpb

Date: 26 Jun, 2013 00:34:54

Message: 5 of 5

On 6/25/2013 4:09 PM, Mary wrote:
...

colHeader = {'Number Full Completion', ...
              'Number Partial Completion Less than Five', ...
              'Number Partial Completion Less than Ten', ...
              'Number Partial Completion More than Ten', ...
              'Number No Login'};
stats(1) = totalCompleted;
stats(2) = partialUnderFive;
stats(3) = partialUnderTen;
stats(4) = partialOverTen;

%% Saves Data into an xls spreadsheet
saveName = ['Module_Status_Report' date]
xlswrite(saveName,log,1)
xlswrite(saveName,stats,2,'A2')
xlswrite(saveName,colHeader,2,'A1')

>>
>> I've never used xlswrite() so no help there, really, sorry...
...

But, as a follow-up, I did the following

 >> colHeader = {'Number Full Completion', ...
              'Number Partial Completion Less than Five', ...
              'Number Partial Completion Less than Ten', ...
              'Number Partial Completion More than Ten', ...
              'Number No Login'};
 >> stats=randperm(100); stats=stats(1:5)
stats =
     80 56 49 48 76
 >> xlswrite('xlswr.xls',stats,2,'A2')
 >> xlswrite('xlswr.xls',colHeader,2,'A1')

And all seemed as expected. I then shortened stats to have only four
entries and cleared the A2:A5 in case that were somehow a problem

 >> stats(5)=[];
 >> xlswrite('xlswr.xls',stats,2,'A2')
 >>

Still as expected.

Then tried as combined

xlswrite('xlswr.xls',{colHeader;stats},2,'A2')

Didn't work; the cell array is complex...have to have a cell array that
is each cell a single entity for xlswrite() to handle it.

If debugging on the above doesn't bring nirvana you might try the
alternate outputs from xlswrite and see what, if any, error message(s)
are generated as klews...

OTOMH looks like what you have should be ok. Nothing like you've got
password protection on the sheet or something, is it???

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us