Help with unstack function on matlab

8 次查看(过去 30 天)
I'm unable to figure out a way to use the unstack function for my data set. The task for my data set was set up where a subject was prompted 3 times in a day and I want to separate the data from the dates column for each day. I've attached a sample example of what I want to look like and also the data set I actually want to work with. Also attached the code that I wrote to try to figure it out on my own.
i=[3:53];
Survey_Prompt_Unstack = unstack(Survey_Prompt, 3, i);
Each time I try to run the command above, I get this message:
%Error using tabular/unstack
%The indicator variable must be specified as a single variable.
  2 个评论
Jan
Jan 2022-7-7
What is the contents of the variable "Survey_Prompt"? Mostlikely you have imported the XLSX file with the same name, but how?
BA
BA 2022-7-7
This is just a part of the code, not the full thing. I set a path and imported it

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2022-7-7
I am not certain what you want.
This is the best I can do with the provided data —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1057225/Survey_Prompt.xlsx', 'VariableNamingRule','preserve')
Warning: Table variable names were truncated to the length namelengthmax. The original names are saved in the VariableDescriptions property.
T1 = 8×53 table
UserId SurveyName SurveyStartedDate SurveySubmittedDate 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 InThePastHourDidAnyOfTheFollowingHappen_ISawHeroinOrOpioids InThePastHourDidAnyOfTheFollowingHappen_SomeoneOfferedMeHeroinO InThePastHourDidAnyOfTheFollowingHappen_OutOfTheBlueIFeltTempte InThePastHourDidAnyOfTheFollowingHappen_IHandled10DollarsOrMore InThePastHourDidAnyOfTheFollowingHappen_IWantedToSeeWhatWouldHa InThePastHourDidAnyOfTheFollowingHappen_IUsedHeroinOrOpioids HowManyBagsPillsDidYouUse InThePastHourDidYouUseAnyOfTheFollowing HowMuchMoneysWorthOfCocaineDidYouHave HowManyDrinksDidYouHave HowManyBenzodiazepineOrBenzosPills HowManyJointsDidYouHave WhatDidYouUse WhereAreYouRightNow RightBeforeYouStartedThisSurveyWhoWereYouWith ChooseBelowWhoYouWereWith_CheckAllThatApply HowCloseOrConnectedDoYouFeelWithThesePeopleRightNow HaveYouEverUseUsedDrugsWithAtLeastOneOfThesePeopleBefore RightBeforeYouStartedThisSurveyWhatWereYouDoing_CheckAllThatApp _____________________ ___________________ _________________ ___________________ ____________________________ ____________________________________ ____________________________________________ ___________________________________________________ ______________________________ ____________________________________________ __________________________________________ ________________________________ ________________________________ __________________________________ ___________________________________ ______________________________ _______________________________ _____________________________ _________________________________ _________________________________________________ _________________________________ _______________________________ ___________________________________ ______________________________________ _________________________________________ _________________________________________________ _________________________________________ ________________________________________________ _________________________________________________ ___________________________________________ __________________________________ __________________________________________________________ ____________________________________________________ _________________________________________________ ___________________________________________________________ _______________________________________________________________ _______________________________________________________________ _______________________________________________________________ _______________________________________________________________ ____________________________________________________________ _________________________ _______________________________________ _____________________________________ _______________________ __________________________________ _______________________ _____________________ ___________________ _____________________________________________ ___________________________________________ ___________________________________________________ ________________________________________________________ _______________________________________________________________ {'213hj123jl12132nm'} {'Question_Prompt'} 05-Aug-2020 05-Aug-2020 0 92 55 17 18 6 0 0 12 78 0 0 0 0 69 6 0 0 0 12 0 0 0 11 NaN 0 0 0 18 NaN {'No'} {'No'} {'No'} {'No'} {'No'} {'No'} NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'At home'} {'Alone'} {'Peer (not friend or acquaintance)'} {'CONDITION_SKIPPED'} NaN {'Working (job or school work)' } {'213hj123jl12132nm'} {'Question_Prompt'} 06-Aug-2020 06-Aug-2020 0 77 68 10 22 22 14 22 17 86 0 10 8 0 78 7 0 14 0 9 0 0 0 10 NaN 0 0 0 14 NaN {'No'} {'No'} {'No'} {'No'} {'No'} {'No'} NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'At home'} {'Alone'} {'Peer (not friend or acquaintance)'} {'CONDITION_SKIPPED'} NaN {'Working (job or school work)' } {'213hj123jl12132nm'} {'Question_Prompt'} 06-Aug-2020 06-Aug-2020 0 81 31 0 17 32 8 23 11 88 0 6 8 0 79 14 0 31 0 0 0 0 0 12 NaN 0 0 0 23 NaN {'No'} {'No'} {'No'} {'No'} {'No'} {'No'} NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'At home'} {'Alone'} {'Peer (not friend or acquaintance)'} {'CONDITION_SKIPPED'} NaN {'Eating' } {'213hj123jl12132nm'} {'Question_Prompt'} 07-Aug-2020 07-Aug-2020 0 76 30 4 8 23 0 6 0 81 0 0 0 0 80 5 0 13 0 0 0 0 0 0 NaN 0 0 0 0 NaN {'No'} {'No'} {'No'} {'No'} {'No'} {'No'} NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'At work'} {'Alone'} {'Peer (not friend or acquaintance)'} {'CONDITION_SKIPPED'} NaN {'Working (job or school work)' } {'213hj123jl12132nm'} {'Question_Prompt'} 07-Aug-2020 07-Aug-2020 0 81 21 6 7 21 8 9 12 79 0 0 0 0 77 6 0 12 0 0 0 0 0 3 NaN 0 0 0 17 NaN {'No'} {'No'} {'No'} {'No'} {'No'} {'No'} NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'At work'} {'Alone'} {'Peer (not friend or acquaintance)'} {'CONDITION_SKIPPED'} NaN {'Working (job or school work)' } {'213hj123jl12132nm'} {'Question_Prompt'} 07-Aug-2020 07-Aug-2020 14 51 21 43 47 52 50 66 50 40 0 46 56 57 54 70 52 53 53 62 2 0 0 51 NaN 0 0 0 55 NaN {'No'} {'No'} {'No'} {'No'} {'No'} {'No'} NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'At home'} {'Alone'} {'Peer (not friend or acquaintance)'} {'CONDITION_SKIPPED'} NaN {'Eating' } {'213hj123jl12132nm'} {'Question_Prompt'} 07-Aug-2020 07-Aug-2020 6 62 49 11 67 58 29 53 49 34 0 0 39 56 74 77 38 50 51 53 2 0 0 46 NaN 0 0 0 53 NaN {'No'} {'No'} {'No'} {'No'} {'No'} {'No'} NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'At home'} {'Alone'} {'Peer (not friend or acquaintance)'} {'CONDITION_SKIPPED'} NaN {'Watching TV/listening to music/playing video games'} {'213hj123jl12132nm'} {'Question_Prompt'} 08-Aug-2020 08-Aug-2020 2 84 75 2 48 5 0 48 8 52 0 0 0 0 63 75 2 25 23 14 0 0 0 21 NaN 0 0 0 32 NaN {'No'} {'No'} {'No'} {'No'} {'No'} {'No'} NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'At home'} {'Alone'} {'Peer (not friend or acquaintance)'} {'CONDITION_SKIPPED'} NaN {'Watching TV/listening to music/playing video games'}
VN = T1.Properties.VariableNames;
T1e = T1(:,[1 3 4 5]); % Eliminate 'SurveyName' Variable
T1u = unstack(T1e,VN{3},VN{4})
Warning: Table variable names that were not valid MATLAB identifiers have been modified. Since table variable names must be unique, any table variable names that happened to match the new identifiers also have been modified.
To use the original INDVAR values as table variable names, set 'VariableNamingRule' to 'preserve'.
T1u = 4×6 table
UserId RightNowHowMuchDoYouFeel_Sad x05_Aug_2020 x06_Aug_2020 x07_Aug_2020 x08_Aug_2020 _____________________ ____________________________ ____________ ____________ ____________ ____________ {'213hj123jl12132nm'} 0 05-Aug-2020 06-Aug-2020 07-Aug-2020 NaT {'213hj123jl12132nm'} 14 NaT NaT 07-Aug-2020 NaT {'213hj123jl12132nm'} 6 NaT NaT 07-Aug-2020 NaT {'213hj123jl12132nm'} 2 NaT NaT NaT 08-Aug-2020
Experiment with this to get the result you want.
.

更多回答(1 个)

Jan
Jan 2022-7-7
According to the documentation of unstack the 3rd input must be a scalar, CHAR vector or string. You provide the vector 1:53 instead.

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by