retriving a text file content information in Matlab












1















I have a .txt file which includes 300 lines. For example the first line is:



ANSWER: correct: yes, time: 6.880674, guess: Lay, action: Lay, file: 16


or the second line is:



ANSWER: correct: no, time: 7.150422, guess: Put on top, action: Stir, file: 18


Only 'time' and 'file' values are numbers and the others are string.



I want to store the values of "correct", "time", "guess", "action" and "file" of the whole 300 lines in the different variables (like some arrays).



How can I do this in the Matlab?










share|improve this question



























    1















    I have a .txt file which includes 300 lines. For example the first line is:



    ANSWER: correct: yes, time: 6.880674, guess: Lay, action: Lay, file: 16


    or the second line is:



    ANSWER: correct: no, time: 7.150422, guess: Put on top, action: Stir, file: 18


    Only 'time' and 'file' values are numbers and the others are string.



    I want to store the values of "correct", "time", "guess", "action" and "file" of the whole 300 lines in the different variables (like some arrays).



    How can I do this in the Matlab?










    share|improve this question

























      1












      1








      1








      I have a .txt file which includes 300 lines. For example the first line is:



      ANSWER: correct: yes, time: 6.880674, guess: Lay, action: Lay, file: 16


      or the second line is:



      ANSWER: correct: no, time: 7.150422, guess: Put on top, action: Stir, file: 18


      Only 'time' and 'file' values are numbers and the others are string.



      I want to store the values of "correct", "time", "guess", "action" and "file" of the whole 300 lines in the different variables (like some arrays).



      How can I do this in the Matlab?










      share|improve this question














      I have a .txt file which includes 300 lines. For example the first line is:



      ANSWER: correct: yes, time: 6.880674, guess: Lay, action: Lay, file: 16


      or the second line is:



      ANSWER: correct: no, time: 7.150422, guess: Put on top, action: Stir, file: 18


      Only 'time' and 'file' values are numbers and the others are string.



      I want to store the values of "correct", "time", "guess", "action" and "file" of the whole 300 lines in the different variables (like some arrays).



      How can I do this in the Matlab?







      matlab






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 17:12









      TaranehTaraneh

      756




      756
























          2 Answers
          2






          active

          oldest

          votes


















          3














          Option 1:



          You can use textscan with the following formatSpec:



          formatSpec = 'ANSWER: correct:%s time:%f guess:%s action:%s file: %f';
          data = textscan(fileID,formatSpec,'Delimiter',',');


          where fileID is the file identifier obtained by fopen.



          Option 2:



          Another option is to use readtable, with the formatting above (directly with the file name, no fileID):



          data = readtable('53485991.txt','Format',formatSpec,'Delimiter',',',...
          'ReadVariableNames',false);
          % the next lines are just to give the table variables some meaningful names:
          varNames = strsplit(fmt,{'ANSWER',':','%s',' ','%f'});
          data.Properties.VariableNames = varNames(2:end-1);


          The result (ignore the values, as I messed that example a little bit while playing with it):



          data =
          4×5 table
          correct time guess action file
          _______ ______ _______________ ______ ____
          'yes' 6.8888 'Lay' 'Lay' 16
          'no' 7.8762 'Put on top' 'Stir' 18
          'no' 7.1503 'Put on bottom' 'Stir' 3
          'no' 7.151 'go' 'Stir' 270


          The advantage in option 2 is that a table is a much more convenient way to hold these data than a cell array (which is the output of textscan).






          share|improve this answer


























          • Learned something new. The textscan is elegant.

            – Banghua Zhao
            Nov 26 '18 at 18:34











          • @BanghuaZhao readtable is even better...

            – EBH
            Nov 26 '18 at 18:35











          • Thanks for the answer. When I use "option 1", data will be appeared in such a form: data = {1x1 cell} [6.8807] {1x1 cell} {1x1 cell} [0x1 double], but how can I retrive my information like time,correct,etc from this data?

            – Taraneh
            Nov 26 '18 at 19:12













          • First, it's better to use option 2. Second, you should type: correct = data{1}, time = data{2} and so on until 5 (file), for each field in your data.

            – EBH
            Nov 26 '18 at 19:21






          • 1





            You have to check that your data is formatted correctly, exactly as you wrote it above. Another option is of encoding issue, but it's hard to tell from the info in this error.

            – EBH
            Nov 26 '18 at 20:24



















          1














          Use fgetl to get a line of the file and while loop to read all of the lines.



          For each line, use regexp to partition the string into cells by : and , delimiter. Then, use strip to remove leading and trailing whitespace for each cell.



          Here is the solution:



          f = fopen('a.txt');

          aline = fgetl(f);
          i = 1;
          while ischar(aline)
          content = strip(regexp(aline,':|,','split'));
          correct{i} = content{3};
          time(i) = str2double(content{5});
          guess{i}= content{7};
          action{i} = content{9};
          file(i) = str2double(content{11});
          i = i + 1;
          aline = fgetl(f);
          end

          fclose(f);


          Example:



          Suppose a.txt file looks like this



          ANSWER: correct: yes, time: 6.880674, guess: Lay, action: Lay, file: 16
          ANSWER: correct: no, time: 7.150422, guess: Put on top, action: Stir, file: 18


          After executing the script, the results are



          correct =
          1×2 cell array
          'yes' 'no'

          time =
          6.8807 7.1504

          guess =
          1×2 cell array
          'Lay' 'Put on top'

          action =
          1×2 cell array
          'Lay' 'Stir'

          file =
          16 18





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53485991%2fretriving-a-text-file-content-information-in-matlab%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            Option 1:



            You can use textscan with the following formatSpec:



            formatSpec = 'ANSWER: correct:%s time:%f guess:%s action:%s file: %f';
            data = textscan(fileID,formatSpec,'Delimiter',',');


            where fileID is the file identifier obtained by fopen.



            Option 2:



            Another option is to use readtable, with the formatting above (directly with the file name, no fileID):



            data = readtable('53485991.txt','Format',formatSpec,'Delimiter',',',...
            'ReadVariableNames',false);
            % the next lines are just to give the table variables some meaningful names:
            varNames = strsplit(fmt,{'ANSWER',':','%s',' ','%f'});
            data.Properties.VariableNames = varNames(2:end-1);


            The result (ignore the values, as I messed that example a little bit while playing with it):



            data =
            4×5 table
            correct time guess action file
            _______ ______ _______________ ______ ____
            'yes' 6.8888 'Lay' 'Lay' 16
            'no' 7.8762 'Put on top' 'Stir' 18
            'no' 7.1503 'Put on bottom' 'Stir' 3
            'no' 7.151 'go' 'Stir' 270


            The advantage in option 2 is that a table is a much more convenient way to hold these data than a cell array (which is the output of textscan).






            share|improve this answer


























            • Learned something new. The textscan is elegant.

              – Banghua Zhao
              Nov 26 '18 at 18:34











            • @BanghuaZhao readtable is even better...

              – EBH
              Nov 26 '18 at 18:35











            • Thanks for the answer. When I use "option 1", data will be appeared in such a form: data = {1x1 cell} [6.8807] {1x1 cell} {1x1 cell} [0x1 double], but how can I retrive my information like time,correct,etc from this data?

              – Taraneh
              Nov 26 '18 at 19:12













            • First, it's better to use option 2. Second, you should type: correct = data{1}, time = data{2} and so on until 5 (file), for each field in your data.

              – EBH
              Nov 26 '18 at 19:21






            • 1





              You have to check that your data is formatted correctly, exactly as you wrote it above. Another option is of encoding issue, but it's hard to tell from the info in this error.

              – EBH
              Nov 26 '18 at 20:24
















            3














            Option 1:



            You can use textscan with the following formatSpec:



            formatSpec = 'ANSWER: correct:%s time:%f guess:%s action:%s file: %f';
            data = textscan(fileID,formatSpec,'Delimiter',',');


            where fileID is the file identifier obtained by fopen.



            Option 2:



            Another option is to use readtable, with the formatting above (directly with the file name, no fileID):



            data = readtable('53485991.txt','Format',formatSpec,'Delimiter',',',...
            'ReadVariableNames',false);
            % the next lines are just to give the table variables some meaningful names:
            varNames = strsplit(fmt,{'ANSWER',':','%s',' ','%f'});
            data.Properties.VariableNames = varNames(2:end-1);


            The result (ignore the values, as I messed that example a little bit while playing with it):



            data =
            4×5 table
            correct time guess action file
            _______ ______ _______________ ______ ____
            'yes' 6.8888 'Lay' 'Lay' 16
            'no' 7.8762 'Put on top' 'Stir' 18
            'no' 7.1503 'Put on bottom' 'Stir' 3
            'no' 7.151 'go' 'Stir' 270


            The advantage in option 2 is that a table is a much more convenient way to hold these data than a cell array (which is the output of textscan).






            share|improve this answer


























            • Learned something new. The textscan is elegant.

              – Banghua Zhao
              Nov 26 '18 at 18:34











            • @BanghuaZhao readtable is even better...

              – EBH
              Nov 26 '18 at 18:35











            • Thanks for the answer. When I use "option 1", data will be appeared in such a form: data = {1x1 cell} [6.8807] {1x1 cell} {1x1 cell} [0x1 double], but how can I retrive my information like time,correct,etc from this data?

              – Taraneh
              Nov 26 '18 at 19:12













            • First, it's better to use option 2. Second, you should type: correct = data{1}, time = data{2} and so on until 5 (file), for each field in your data.

              – EBH
              Nov 26 '18 at 19:21






            • 1





              You have to check that your data is formatted correctly, exactly as you wrote it above. Another option is of encoding issue, but it's hard to tell from the info in this error.

              – EBH
              Nov 26 '18 at 20:24














            3












            3








            3







            Option 1:



            You can use textscan with the following formatSpec:



            formatSpec = 'ANSWER: correct:%s time:%f guess:%s action:%s file: %f';
            data = textscan(fileID,formatSpec,'Delimiter',',');


            where fileID is the file identifier obtained by fopen.



            Option 2:



            Another option is to use readtable, with the formatting above (directly with the file name, no fileID):



            data = readtable('53485991.txt','Format',formatSpec,'Delimiter',',',...
            'ReadVariableNames',false);
            % the next lines are just to give the table variables some meaningful names:
            varNames = strsplit(fmt,{'ANSWER',':','%s',' ','%f'});
            data.Properties.VariableNames = varNames(2:end-1);


            The result (ignore the values, as I messed that example a little bit while playing with it):



            data =
            4×5 table
            correct time guess action file
            _______ ______ _______________ ______ ____
            'yes' 6.8888 'Lay' 'Lay' 16
            'no' 7.8762 'Put on top' 'Stir' 18
            'no' 7.1503 'Put on bottom' 'Stir' 3
            'no' 7.151 'go' 'Stir' 270


            The advantage in option 2 is that a table is a much more convenient way to hold these data than a cell array (which is the output of textscan).






            share|improve this answer















            Option 1:



            You can use textscan with the following formatSpec:



            formatSpec = 'ANSWER: correct:%s time:%f guess:%s action:%s file: %f';
            data = textscan(fileID,formatSpec,'Delimiter',',');


            where fileID is the file identifier obtained by fopen.



            Option 2:



            Another option is to use readtable, with the formatting above (directly with the file name, no fileID):



            data = readtable('53485991.txt','Format',formatSpec,'Delimiter',',',...
            'ReadVariableNames',false);
            % the next lines are just to give the table variables some meaningful names:
            varNames = strsplit(fmt,{'ANSWER',':','%s',' ','%f'});
            data.Properties.VariableNames = varNames(2:end-1);


            The result (ignore the values, as I messed that example a little bit while playing with it):



            data =
            4×5 table
            correct time guess action file
            _______ ______ _______________ ______ ____
            'yes' 6.8888 'Lay' 'Lay' 16
            'no' 7.8762 'Put on top' 'Stir' 18
            'no' 7.1503 'Put on bottom' 'Stir' 3
            'no' 7.151 'go' 'Stir' 270


            The advantage in option 2 is that a table is a much more convenient way to hold these data than a cell array (which is the output of textscan).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 28 '18 at 20:54

























            answered Nov 26 '18 at 18:08









            EBHEBH

            9,46332248




            9,46332248













            • Learned something new. The textscan is elegant.

              – Banghua Zhao
              Nov 26 '18 at 18:34











            • @BanghuaZhao readtable is even better...

              – EBH
              Nov 26 '18 at 18:35











            • Thanks for the answer. When I use "option 1", data will be appeared in such a form: data = {1x1 cell} [6.8807] {1x1 cell} {1x1 cell} [0x1 double], but how can I retrive my information like time,correct,etc from this data?

              – Taraneh
              Nov 26 '18 at 19:12













            • First, it's better to use option 2. Second, you should type: correct = data{1}, time = data{2} and so on until 5 (file), for each field in your data.

              – EBH
              Nov 26 '18 at 19:21






            • 1





              You have to check that your data is formatted correctly, exactly as you wrote it above. Another option is of encoding issue, but it's hard to tell from the info in this error.

              – EBH
              Nov 26 '18 at 20:24



















            • Learned something new. The textscan is elegant.

              – Banghua Zhao
              Nov 26 '18 at 18:34











            • @BanghuaZhao readtable is even better...

              – EBH
              Nov 26 '18 at 18:35











            • Thanks for the answer. When I use "option 1", data will be appeared in such a form: data = {1x1 cell} [6.8807] {1x1 cell} {1x1 cell} [0x1 double], but how can I retrive my information like time,correct,etc from this data?

              – Taraneh
              Nov 26 '18 at 19:12













            • First, it's better to use option 2. Second, you should type: correct = data{1}, time = data{2} and so on until 5 (file), for each field in your data.

              – EBH
              Nov 26 '18 at 19:21






            • 1





              You have to check that your data is formatted correctly, exactly as you wrote it above. Another option is of encoding issue, but it's hard to tell from the info in this error.

              – EBH
              Nov 26 '18 at 20:24

















            Learned something new. The textscan is elegant.

            – Banghua Zhao
            Nov 26 '18 at 18:34





            Learned something new. The textscan is elegant.

            – Banghua Zhao
            Nov 26 '18 at 18:34













            @BanghuaZhao readtable is even better...

            – EBH
            Nov 26 '18 at 18:35





            @BanghuaZhao readtable is even better...

            – EBH
            Nov 26 '18 at 18:35













            Thanks for the answer. When I use "option 1", data will be appeared in such a form: data = {1x1 cell} [6.8807] {1x1 cell} {1x1 cell} [0x1 double], but how can I retrive my information like time,correct,etc from this data?

            – Taraneh
            Nov 26 '18 at 19:12







            Thanks for the answer. When I use "option 1", data will be appeared in such a form: data = {1x1 cell} [6.8807] {1x1 cell} {1x1 cell} [0x1 double], but how can I retrive my information like time,correct,etc from this data?

            – Taraneh
            Nov 26 '18 at 19:12















            First, it's better to use option 2. Second, you should type: correct = data{1}, time = data{2} and so on until 5 (file), for each field in your data.

            – EBH
            Nov 26 '18 at 19:21





            First, it's better to use option 2. Second, you should type: correct = data{1}, time = data{2} and so on until 5 (file), for each field in your data.

            – EBH
            Nov 26 '18 at 19:21




            1




            1





            You have to check that your data is formatted correctly, exactly as you wrote it above. Another option is of encoding issue, but it's hard to tell from the info in this error.

            – EBH
            Nov 26 '18 at 20:24





            You have to check that your data is formatted correctly, exactly as you wrote it above. Another option is of encoding issue, but it's hard to tell from the info in this error.

            – EBH
            Nov 26 '18 at 20:24













            1














            Use fgetl to get a line of the file and while loop to read all of the lines.



            For each line, use regexp to partition the string into cells by : and , delimiter. Then, use strip to remove leading and trailing whitespace for each cell.



            Here is the solution:



            f = fopen('a.txt');

            aline = fgetl(f);
            i = 1;
            while ischar(aline)
            content = strip(regexp(aline,':|,','split'));
            correct{i} = content{3};
            time(i) = str2double(content{5});
            guess{i}= content{7};
            action{i} = content{9};
            file(i) = str2double(content{11});
            i = i + 1;
            aline = fgetl(f);
            end

            fclose(f);


            Example:



            Suppose a.txt file looks like this



            ANSWER: correct: yes, time: 6.880674, guess: Lay, action: Lay, file: 16
            ANSWER: correct: no, time: 7.150422, guess: Put on top, action: Stir, file: 18


            After executing the script, the results are



            correct =
            1×2 cell array
            'yes' 'no'

            time =
            6.8807 7.1504

            guess =
            1×2 cell array
            'Lay' 'Put on top'

            action =
            1×2 cell array
            'Lay' 'Stir'

            file =
            16 18





            share|improve this answer




























              1














              Use fgetl to get a line of the file and while loop to read all of the lines.



              For each line, use regexp to partition the string into cells by : and , delimiter. Then, use strip to remove leading and trailing whitespace for each cell.



              Here is the solution:



              f = fopen('a.txt');

              aline = fgetl(f);
              i = 1;
              while ischar(aline)
              content = strip(regexp(aline,':|,','split'));
              correct{i} = content{3};
              time(i) = str2double(content{5});
              guess{i}= content{7};
              action{i} = content{9};
              file(i) = str2double(content{11});
              i = i + 1;
              aline = fgetl(f);
              end

              fclose(f);


              Example:



              Suppose a.txt file looks like this



              ANSWER: correct: yes, time: 6.880674, guess: Lay, action: Lay, file: 16
              ANSWER: correct: no, time: 7.150422, guess: Put on top, action: Stir, file: 18


              After executing the script, the results are



              correct =
              1×2 cell array
              'yes' 'no'

              time =
              6.8807 7.1504

              guess =
              1×2 cell array
              'Lay' 'Put on top'

              action =
              1×2 cell array
              'Lay' 'Stir'

              file =
              16 18





              share|improve this answer


























                1












                1








                1







                Use fgetl to get a line of the file and while loop to read all of the lines.



                For each line, use regexp to partition the string into cells by : and , delimiter. Then, use strip to remove leading and trailing whitespace for each cell.



                Here is the solution:



                f = fopen('a.txt');

                aline = fgetl(f);
                i = 1;
                while ischar(aline)
                content = strip(regexp(aline,':|,','split'));
                correct{i} = content{3};
                time(i) = str2double(content{5});
                guess{i}= content{7};
                action{i} = content{9};
                file(i) = str2double(content{11});
                i = i + 1;
                aline = fgetl(f);
                end

                fclose(f);


                Example:



                Suppose a.txt file looks like this



                ANSWER: correct: yes, time: 6.880674, guess: Lay, action: Lay, file: 16
                ANSWER: correct: no, time: 7.150422, guess: Put on top, action: Stir, file: 18


                After executing the script, the results are



                correct =
                1×2 cell array
                'yes' 'no'

                time =
                6.8807 7.1504

                guess =
                1×2 cell array
                'Lay' 'Put on top'

                action =
                1×2 cell array
                'Lay' 'Stir'

                file =
                16 18





                share|improve this answer













                Use fgetl to get a line of the file and while loop to read all of the lines.



                For each line, use regexp to partition the string into cells by : and , delimiter. Then, use strip to remove leading and trailing whitespace for each cell.



                Here is the solution:



                f = fopen('a.txt');

                aline = fgetl(f);
                i = 1;
                while ischar(aline)
                content = strip(regexp(aline,':|,','split'));
                correct{i} = content{3};
                time(i) = str2double(content{5});
                guess{i}= content{7};
                action{i} = content{9};
                file(i) = str2double(content{11});
                i = i + 1;
                aline = fgetl(f);
                end

                fclose(f);


                Example:



                Suppose a.txt file looks like this



                ANSWER: correct: yes, time: 6.880674, guess: Lay, action: Lay, file: 16
                ANSWER: correct: no, time: 7.150422, guess: Put on top, action: Stir, file: 18


                After executing the script, the results are



                correct =
                1×2 cell array
                'yes' 'no'

                time =
                6.8807 7.1504

                guess =
                1×2 cell array
                'Lay' 'Put on top'

                action =
                1×2 cell array
                'Lay' 'Stir'

                file =
                16 18






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 '18 at 18:04









                Banghua ZhaoBanghua Zhao

                1,2851719




                1,2851719






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53485991%2fretriving-a-text-file-content-information-in-matlab%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks