Function "Ismember" is giving me an error message

5 Ansichten (letzte 30 Tage)
BA
BA am 19 Jul. 2022
Kommentiert: BA am 19 Jul. 2022
I keep having trouble using the ismember function. I get an error code:
Error using cell/ismember. Input A of class cell and input B of class char must be cell arrays of character vectors, unless one is a character vector.
Here is my code:
temp = [];
for col = 21:50
x = General_Prompt(:,col);
x = table2cell(x);
x(ismember(x,'0 - Not at all')) = {'0'};
x(ismember(x,'50 - Maybe')) = {'50'};
x(ismember(x,'100 - Very much')) = {'100'};
x(ismember(x,'1 - Strongly agree')) = {'1'};
x(ismember(x,'50 - I don''t know')) = {'50'};
x(ismember(x,'100 - Strongly disagree')) = {'100'};
x(ismember(x,'1 - Not at all')) = {'1'};
x(ismember(x,'0 - Very badly')) = {'0'};
x(ismember(x,'50 - Moderately')) = {'50'};
x(ismember(x,'100 - Very well')) = {'100'};
x(ismember(x,'0 - None')) = {'0'};
x(ismember(x,'100 - Completely')) = {'100'};
x = str2double(x);
temp = [temp x];
end
temp = array2table(temp);
temp.Properties.VariableNames = General_Prompt.Properties.VariableNames(21:50);
General_Prompt = [General_Prompt(:,[2:3 6:9 12:13]) temp General_Prompt(:,51:end)];
If someone knows what is causing this, please let me know. I've attached the dataset I'm using for this. I should mention this used to be an old script from 2018 that I'm now using in 2022a and trying to figure out how to convert it to be compatible with the newer version of MATLAB

Akzeptierte Antwort

Chunru
Chunru am 19 Jul. 2022
Does the following help?
A = readtable("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1069865/General_Prompt.xlsx");
head(A)
ans = 8×36 table
MetricWireID SurveyName General_Prompt_SurveyStartedDate General_Prompt_SurveyStartedTime General_Prompt_SurveySubmittedDate General_Prompt_SurveySubmittedTime RightNowHowMuchDoYouFeel_Sad RightNowHowMuchDoYouFeel_InAGoodMood RightNowHowMuchDoYouFeel_ConnectedWithOthers RightNowHowMuchDoYouFeel_PsychologicalEmotionalPain RightNowHowMuchDoYouFeel_Bored RightNowHowMuchDoYouFeel_LikeYouNeedToEscape RightNowHowMuchDoYouFeel_AgitatedIrritated RightNowHowMuchDoYouFeel_Worried RightNowHowMuchDoYouFeel_Anxious RightNowHowMuchDoYouFeel_Energetic RightNowHowMuchDoYouFeel_Selfhatred RightNowHowMuchDoYouFeel_Angry RightNowHowMuchDoYouFeel_OnEdge RightNowHowMuchDoYouFeel_Numb RightNowHowMuchDoYouFeel_Grateful RightNowHowMuchDoYouFeel_PhysicalPainOrDiscomfort RightNowHowMuchDoYouFeel_Hopeless RightNowHowMuchDoYouFeel_Lonely RightNowHowMuchDoYouFeel_Humiliated RightNowHowMuchDoYouFeel_DesireToAvoid RightNowHowMuchDoYouFeel_UrgeToUseAlcohol RightNowHowMuchDoYouFeel_UrgeToUseHeroinOrOpioids RightNowHowMuchDoYouFeel_UrgeToUseCocaine RightNowHowMuchDoYouFeel_UrgeToEatSomethingSweet RightNowHowMuchDoYouFeel_UrgeToEatSomethingSavory RightNowHowMuchDoYouFeel_UrgeToHurtYourself RightNowHowMuchDoYouFeel_UrgeToDie RightNowHowMuchDoYouFeel_UrgeToDoSomethingRiskyOrImpulsive RightNowHowMuchDoYouFeel_NeedToBeWithFriendsOrFamily RightNowIWouldBeAbleToEnjoyThingsThatIUsuallyLike _____________________ ________________________ ________________________________ ________________________________ __________________________________ __________________________________ ____________________________ ____________________________________ ____________________________________________ ___________________________________________________ ______________________________ ____________________________________________ __________________________________________ ________________________________ ________________________________ __________________________________ ___________________________________ ______________________________ _______________________________ _____________________________ _________________________________ _________________________________________________ _________________________________ _______________________________ ___________________________________ ______________________________________ _________________________________________ _________________________________________________ _________________________________________ ________________________________________________ _________________________________________________ ___________________________________________ __________________________________ __________________________________________________________ ____________________________________________________ _________________________________________________ {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 09-Mar-2022 0.57934 09-Mar-2022 0.58215 {'50 - Maybe'} 48 30 {'29'} 62 28 {'23'} 20 33 24 {'23' } {'15' } {'14'} {'11'} 36 14 {'20' } {'43' } {'17'} 67 {'3' } {'67'} {'0 - Not at all'} 76 39 {'1' } {'4' } {'2' } 1 46 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 09-Mar-2022 0.65731 09-Mar-2022 0.65907 {'51' } NaN 29 {'30'} 36 49 {'27'} 52 26 24 {'55' } {'34' } {'25'} {'13'} 16 55 {'33' } {'50 - Maybe'} {'11'} 64 {'9' } {'67'} {'3' } 19 24 {'3' } {'4' } {'5' } 3 59 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 09-Mar-2022 0.74525 09-Mar-2022 0.74794 {'29' } 22 22 {'33'} 27 27 {'28'} 55 61 15 {'17' } {'24' } {'19'} {'24'} 18 61 {'26' } {'19' } {'20'} 67 {'5' } {'74'} {'6' } 18 30 {'18' } {'2' } {'5' } 6 47 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 10-Mar-2022 0.60737 10-Mar-2022 0.60931 {'38' } 35 30 {'22'} 15 16 {'24'} 28 24 32 {'24' } {'16' } {'14'} {'14'} 11 25 {'35' } {'20' } {'9' } 24 {'7' } {'63'} {'0 - Not at all'} 21 33 {'3' } {'1' } {'0 - Not at all'} 51 57 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 10-Mar-2022 0.64112 10-Mar-2022 0.64244 {'10' } 10 4 {'7' } 18 13 {'75'} 15 15 52 {'14' } {'74' } {'27'} {'17'} 13 51 {'50 - Maybe'} {'9' } {'19'} 23 {'8' } {'73'} {'0 - Not at all'} 45 32 {'0 - Not at all'} {'0 - Not at all'} {'2' } NaN 53 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 10-Mar-2022 0.66376 10-Mar-2022 0.66542 {'52' } 51 51 {'53'} 48 43 {'56'} 61 56 47 {'50 - Maybe'} {'50 - Maybe'} {'48'} {'51'} 46 49 {'42' } {'39' } {'35'} 25 {'4' } {'71'} {'2' } 66 66 {'1' } {'0 - Not at all'} {'31' } 63 55 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 11-Mar-2022 0.60443 11-Mar-2022 0.61084 {'15' } 56 10 {'27'} 40 9 {'6' } 58 53 28 {'7' } {'15' } {'15'} {'5' } 5 41 {'28' } {'18' } {'15'} 20 {'0 - Not at all'} {'70'} {'1' } 61 NaN {'0 - Not at all'} {'0 - Not at all'} {'1' } 6 48 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 11-Mar-2022 0.67159 11-Mar-2022 0.67269 {'19' } 26 28 {'26'} 26 18 {'32'} 27 24 23 {'24' } {'29' } {'22'} {'25'} 25 19 {'21' } {'21' } {'24'} 23 {'21' } {'21'} {'30' } 28 45 {'34' } {'43' } {'50 - Maybe' } 64 41
for col = 7:size(A,2) % from column 7 onwards
x = A{:, col};
if iscell(x)
x = cellfun(@(v) sscanf(v, '%f'), x);
A.(A.Properties.VariableNames{col}) = x;
end
end
A
A = 73×36 table
MetricWireID SurveyName General_Prompt_SurveyStartedDate General_Prompt_SurveyStartedTime General_Prompt_SurveySubmittedDate General_Prompt_SurveySubmittedTime RightNowHowMuchDoYouFeel_Sad RightNowHowMuchDoYouFeel_InAGoodMood RightNowHowMuchDoYouFeel_ConnectedWithOthers RightNowHowMuchDoYouFeel_PsychologicalEmotionalPain RightNowHowMuchDoYouFeel_Bored RightNowHowMuchDoYouFeel_LikeYouNeedToEscape RightNowHowMuchDoYouFeel_AgitatedIrritated RightNowHowMuchDoYouFeel_Worried RightNowHowMuchDoYouFeel_Anxious RightNowHowMuchDoYouFeel_Energetic RightNowHowMuchDoYouFeel_Selfhatred RightNowHowMuchDoYouFeel_Angry RightNowHowMuchDoYouFeel_OnEdge RightNowHowMuchDoYouFeel_Numb RightNowHowMuchDoYouFeel_Grateful RightNowHowMuchDoYouFeel_PhysicalPainOrDiscomfort RightNowHowMuchDoYouFeel_Hopeless RightNowHowMuchDoYouFeel_Lonely RightNowHowMuchDoYouFeel_Humiliated RightNowHowMuchDoYouFeel_DesireToAvoid RightNowHowMuchDoYouFeel_UrgeToUseAlcohol RightNowHowMuchDoYouFeel_UrgeToUseHeroinOrOpioids RightNowHowMuchDoYouFeel_UrgeToUseCocaine RightNowHowMuchDoYouFeel_UrgeToEatSomethingSweet RightNowHowMuchDoYouFeel_UrgeToEatSomethingSavory RightNowHowMuchDoYouFeel_UrgeToHurtYourself RightNowHowMuchDoYouFeel_UrgeToDie RightNowHowMuchDoYouFeel_UrgeToDoSomethingRiskyOrImpulsive RightNowHowMuchDoYouFeel_NeedToBeWithFriendsOrFamily RightNowIWouldBeAbleToEnjoyThingsThatIUsuallyLike _____________________ ________________________ ________________________________ ________________________________ __________________________________ __________________________________ ____________________________ ____________________________________ ____________________________________________ ___________________________________________________ ______________________________ ____________________________________________ __________________________________________ ________________________________ ________________________________ __________________________________ ___________________________________ ______________________________ _______________________________ _____________________________ _________________________________ _________________________________________________ _________________________________ _______________________________ ___________________________________ ______________________________________ _________________________________________ _________________________________________________ _________________________________________ ________________________________________________ _________________________________________________ ___________________________________________ __________________________________ __________________________________________________________ ____________________________________________________ _________________________________________________ {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 09-Mar-2022 0.57934 09-Mar-2022 0.58215 50 48 30 29 62 28 23 20 33 24 23 15 14 11 36 14 20 43 17 67 3 67 0 76 39 1 4 2 1 46 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 09-Mar-2022 0.65731 09-Mar-2022 0.65907 51 NaN 29 30 36 49 27 52 26 24 55 34 25 13 16 55 33 50 11 64 9 67 3 19 24 3 4 5 3 59 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 09-Mar-2022 0.74525 09-Mar-2022 0.74794 29 22 22 33 27 27 28 55 61 15 17 24 19 24 18 61 26 19 20 67 5 74 6 18 30 18 2 5 6 47 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 10-Mar-2022 0.60737 10-Mar-2022 0.60931 38 35 30 22 15 16 24 28 24 32 24 16 14 14 11 25 35 20 9 24 7 63 0 21 33 3 1 0 51 57 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 10-Mar-2022 0.64112 10-Mar-2022 0.64244 10 10 4 7 18 13 75 15 15 52 14 74 27 17 13 51 50 9 19 23 8 73 0 45 32 0 0 2 NaN 53 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 10-Mar-2022 0.66376 10-Mar-2022 0.66542 52 51 51 53 48 43 56 61 56 47 50 50 48 51 46 49 42 39 35 25 4 71 2 66 66 1 0 31 63 55 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 11-Mar-2022 0.60443 11-Mar-2022 0.61084 15 56 10 27 40 9 6 58 53 28 7 15 15 5 5 41 28 18 15 20 0 70 1 61 NaN 0 0 1 6 48 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 11-Mar-2022 0.67159 11-Mar-2022 0.67269 19 26 28 26 26 18 32 27 24 23 24 29 22 25 25 19 21 21 24 23 21 21 30 28 45 34 43 50 64 41 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 11-Mar-2022 0.67733 11-Mar-2022 0.67868 27 30 31 36 31 31 29 22 35 35 35 37 33 37 35 41 50 49 56 52 1 72 1 34 49 0 1 4 49 50 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 12-Mar-2022 0.52457 12-Mar-2022 0.52628 21 27 39 31 39 24 33 27 58 42 9 49 25 16 53 27 22 21 15 6 0 68 1 24 36 2 1 6 9 46 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 12-Mar-2022 0.64763 12-Mar-2022 0.64926 36 27 30 27 27 27 38 29 35 35 29 34 33 32 32 27 29 41 32 39 3 58 1 49 57 0 1 2 45 38 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 12-Mar-2022 0.69402 12-Mar-2022 0.69516 6 24 39 7 7 12 3 18 NaN 40 14 20 15 18 11 32 13 4 4 7 0 56 1 7 NaN 1 4 2 20 39 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 13-Mar-2022 0.48693 13-Mar-2022 0.48904 22 17 34 39 38 41 57 56 47 19 41 31 39 46 39 49 47 53 61 61 3 55 6 56 56 0 1 51 57 53 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 13-Mar-2022 0.81632 13-Mar-2022 0.81744 11 18 19 36 17 45 34 42 45 45 23 46 22 45 55 42 48 50 45 40 4 61 1 43 40 3 0 51 31 40 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 14-Mar-2022 0.79081 14-Mar-2022 0.79255 46 56 55 45 55 52 54 56 57 59 58 56 53 56 58 62 43 34 33 37 4 54 2 5 47 2 3 2 38 44 {'8as7da0s9d8a9sd8a'} {'Questionnaire_Prompt'} 15-Mar-2022 0.49403 15-Mar-2022 0.49538 44 44 45 41 47 38 41 45 29 40 36 33 45 28 33 36 26 16 27 28 1 47 4 43 51 3 1 50 46 42
  1 Kommentar
BA
BA am 19 Jul. 2022
Yes it does, thanks a lot!
I just had to add 'UniformOutput', false at the end
for col = 8:size(General_Prompt,2) % from column 8 onwards
x = General_Prompt{:, col};
if iscell(x)
x = cellfun(@(v) sscanf(v, '%f'), x, 'UniformOutput', false);
General_Prompt.(General_Prompt.Properties.VariableNames{col}) = x;
end
end
The rest of it is perfect and exactly what I needed. Can't thank you enough

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by