Storing Matlab data and strings in a tabulated file

392 views Asked by At

I am creating a program which opens an image, and uses the MATLAB ginput command to store x and y coordinates, which are operated on in the loop to fulfill requirements of an if statement and output a number or string corresponding to the region clicked during the ginput session. At the same time, I am using the input command to input a string from the command window relating to these numbers. The ginput session is placed in a while loop so a click in a specific area will end the input session. For each session (while loop), only one or two inputs from the command window are needed. Finally, I am trying to store all the data in a csv or txt file, but I would like it to be tabulated so it is easy to read, i.e. rows and columns with headers. I am including some sample code. My questions are: 1, how can an input of x and y coordinates be translated to a string? It is simple to do this for a number, but I cannot get it to work with a string. 2, any help on printing the strings and number to a tabulated text or cdv file would be appreciated.

Command line input:

prompt='Batter:';
Batter=input(prompt,'s');

While Loop:

count=1;
flag=0;
while(flag==0)



    [x,y]= ginput(1);

    if (y>539)
        flag=1;
    end

    if x<594 && x>150 && y<539 && y>104
        %it's in the square
        X=x;
        Y=y;
    end
    if x<524 && x>207 && y<480 && y>163
       result='strike'
    else
        result='ball'
    end

    [x,y]= ginput(1);

    pitch=0;
    if x<136 && x>13
        %its' pitch column
        if y<539
            pitch=6;

        end
        if y<465
            pitch=5;

        end
        if y<390
            pitch=4;

        end
        if y<319
            pitch=3;

        end
        if y<249
            pitch=2;

        end
        if y<175
            pitch=1;
        end
    end

    if pitch==0

    else

        plot(X,Y,'o','MarkerFaceColor',colors(pitch),'MarkerSize',25);
        text(X,Y,mat2str(count));

    end
    count=count+1

    M(count,:)=[X,Y,pitch];
end

For the above series of if statements, I would prefer a string output rather than the numbers 1-6 if the condition is satisfied. The fprintf function is used to print to a file, but I have issues combining the strings and numbers using it:

fileID = fopen('pitches.csv','w');

fid = fopen('gamedata.txt','w');
fmtString = [repmat('%s\t',1,size(Batter,2)-1),'%s\n'];
fprintf(fid,fmtString,Batter,result);
fclose(fid);

for i=1:length(M)
    fprintf(fileID,'%6.2f %6.2f %d\n',M(i,1),M(i,2),M(i,3));
end

fclose(fileID);

I have tried adding the string handles to the fprintf command along with the columns of M, but get errors. I either need to store them in an array (How?) and print all the array columns to the file, or use some other method. I also tried a version of the writetable method:

 writetable(T,'tabledata2.txt','Delimiter','\t','WriteRowNames',true)

but I can't get everything to work right. Thanks very much for any help.

1

There are 1 answers

1
rayryeng On

Let's tackle your questions one at a time:

1, how can an input of x and y coordinates be translated to a string?

You can use the sprintf command in MATLAB. This takes exactly the same syntax as fprintf, but the output of this function will give you a string / character array of whatever you desire.

2, any help on printing the strings and number to a tabulated text or cdv file would be appreciated.

You can still use fprintf but you can specify a matrix as the input. As such, you can do this:

fprintf(fileID,'%6.2f %6.2f %d\n', M.');

This will write the entire matrix to file. However, care must be taken here because MATLAB writes to files in column major format. This means that it will traverse along the rows before going to the next column. If you want to write data row by row, you will need to transpose the matrix first so that when you are traversing down the rows, it will basically do what you want. You will need to keep this in mind before you start trying to write strings to an file. What I would recommend is that you place each string in a cell array, then loop through each element in the cell array and write each string individually line by line.

Hopefully this helps push you in the right direction. Reply back to me in a comment and we can keep talking if you need more help.