How to make each component a string in one whole string?

2 visualizaciones (últimos 30 días)
Han
Han el 29 de Abr. de 2020
Comentada: Han el 29 de Abr. de 2020
I have an excel file that transfers over letters in a string like {'C D E F'}. I was wondering how to make each letter a string by itself, like 'C' 'D' 'E' 'F'.
Here's my code not sure if it will help or not.
%Paino Figure w/ pushbuttons
function PianoImage_HBraslawsce()
f=figure();
set(f, 'MenuBar','none'); %Remove menu bar
set(f,'NumberTitle','off');
set(f,'Name', 'Note Learner'); %Change name
%Each key as a pushbutton
%<SM:PLOT>
KEYC = uicontrol('style','pushbutton','backgroundcolor',[1 1 1],'units','normalized',...
'position',[.15 .15 .1 .45], 'UserData', 'C','Callback',@keypressed);
KEYD = uicontrol('style','pushbutton','backgroundcolor',[1 1 1],'units','normalized',...
'position',[.25 .15 .1 .45], 'UserData', 'D','Callback',@keypressed);
KEYE = uicontrol('style','pushbutton','backgroundcolor',[1 1 1],'units','normalized',...
'position',[.35 .15 .1 .45], 'UserData', 'E','Callback',@keypressed);
KEYF = uicontrol('style','pushbutton','backgroundcolor',[1 1 1],'units','normalized',...
'position',[.45 .15 .1 .45], 'UserData', 'F','Callback',@keypressed);
KEYG = uicontrol('style','pushbutton','backgroundcolor',[1 1 1],'units','normalized',...
'position',[.55 .15 .1 .45], 'UserData', 'G','Callback',@keypressed);
KEYA = uicontrol('style','pushbutton','backgroundcolor',[1 1 1],'units','normalized',...
'position',[.65 .15 .1 .45], 'UserData', 'A','Callback',@keypressed);
KEYB = uicontrol('style','pushbutton','backgroundcolor',[1 1 1],'units','normalized',...
'position',[.75 .15 .1 .45], 'UserData', 'B','Callback',@keypressed);
KEYCHI = uicontrol('style','pushbutton','backgroundcolor',[1 1 1],'units','normalized',...
'position',[.85 .15 .1 .45], 'UserData', 'C(high)','Callback',@keypressed);
KEYCSH = uicontrol('style','pushbutton','backgroundcolor',[0 0 0],'units','normalized',...
'position',[.20 .3 .08 .3], 'UserData', 'C#','Callback',@keypressed);
KEYEb=uicontrol('style','pushbutton','backgroundcolor',[0 0 0],'units','normalized',...
'position',[.30 .3 .08 .3], 'UserData', 'Eb','Callback',@keypressed);
KEYFSH=uicontrol('style','pushbutton','backgroundcolor',[0 0 0],'units','normalized',...
'position',[.5 .3 .08 .3], 'UserData', 'F#','Callback',@keypressed);
KEYAb=uicontrol('style','pushbutton','backgroundcolor',[0 0 0],'units','normalized',...
'position',[.60 .3 .08 .3], 'UserData', 'Ab','Callback',@keypressed);
KEYBb=uicontrol('style','pushbutton','backgroundcolor',[0 0 0],'units','normalized',...
'position',[.70 .3 .08 .3], 'UserData', 'Bb','Callback',@keypressed);
%MATLAB read data
[~,~,Array]=xlsread('Note_File'); %<SM:READ>
%slices out notes only
Notes=Array(1:3,2:6); %<SM:SLICE>
Beginner=Notes(1:1,1:5); %slices out each level
Intermediate=Notes(2:2,1:5);
Advanced=Notes(3:3,1:5);
Beginner2=numel(Beginner); %randomizes beginner level
set1=randperm(Beginner2); %<SM:RANDUSE>
BeginnerRAN=Beginner(set1(1:1));
Intermediate2=numel(Intermediate); %randomizes intermediate level
set2=randperm(Intermediate2);
IntermediateRAN=Intermediate(set2(1:1));
Advanced2=numel(Advanced); %randomizes advanced level
set3=randperm(Advanced2);
AdvancedRAN=Advanced(set3(1:1));
randomKeysToPress = BeginnerRAN %this is where it stops working
for key = randomKeysToPress %<SM:FOR>
fprintf(' %s', char(key));
end
fprintf('\n');
atKey=1;
function keypressed(hObject, eventdata)
if strcmp(randomKeysToPress{atKey}, get(hObject, 'UserData'))==1
msgbox('Good Job!');
atKey = atKey + 1;
if atKey > length(randomKeysToPress)
fprintf('Game over!!!\n');
end
else
msgbox('ERROR. Please try again. \n');
end
end
end
  1 comentario
Rik
Rik el 29 de Abr. de 2020
What is your exact input? The cell containing a char array with space delimited letters?
And what is your exact output? Do you want a char array with just the letters? Should each letter be a char wrapped in a cell?

Iniciar sesión para comentar.

Respuestas (2)

jessupj
jessupj el 29 de Abr. de 2020
depends on how you want your output, but i'd use the split function and then cast the returned cell into a string array
Snew = string( split(S,' ') )
Snew{3}
ans =
'E'
  3 comentarios
Rik
Rik el 29 de Abr. de 2020
They are already stored in a cell array. From your code it is not clear to me how you can best use it, but you don't need to hard-code indices.
Han
Han el 29 de Abr. de 2020
@ jessupj, actually I got it to work! Thank you soooo much!

Iniciar sesión para comentar.


David Hill
David Hill el 29 de Abr. de 2020
I am not sure what you are doing, but:
x={'C D E F'};
y=cell2mat(x);
y=y(y~=' ')';%it is now a column matrix, but you could convert back to a cell array

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by