MATLAB HELP--URGENT

edited August 2009 in Science & Tech
Hi, I am trying to find a function or code which will do the following:

Search an array for a specific violation I have coded. When the violation is found it is printed to an Excel sheet along with the time and value just before and just after the violation.

The problem I am having is that sometimes there is no data value (only a time) either right before or right after the violation. The way I have my code is that it just prints the value before and after using i-1/i+1 in a loop (i being the row in the data where the violation occurred). This is resulting in an empty cell in my printed Excel sheet if there happens to be no data. How can I create a loop so that it searches for the cell just before and just after the violations that CONTAINS data.

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited August 2009
    can you post a snippet from the array so i can see what you mean?
  • edited August 2009
    Here is my main loop. The lines with stars are where I am trying to do what I described in my first post. Obviously, I have to take into account the fact that the error may be in the first or last cell of the array, which explains the extra IF statements (i>1 and i<length(A.data(j).value.d(:)))


    k=1;
    for j = 1:6
    if Checks(j)
    for i = 1:length(A.data(j).value.d(:))
    if A.data(j).value.d(i) >= Values(1,j);
    Results{j}{k,1} = sectogmt(A.data(j).value.t(i));
    Results{j}{k,2} = A.data(j).value.d(i);
    if i>1
    * if isempty(i-1)
    * i=i-1;
    else
    Results{j}{k,3} = sectogmt(A.data(j).value.t(i-1));
    Results{j}{k,4} = A.data(j).value.d(i-1);
    end
    end
    if i<length(A.data(j).value.d(:))
    * if isempty(i+1)
    * i=i+1;
    else
    Results{j}{k,5} = sectogmt(A.data(j).value.t(i+1));
    Results{j}{k,6} = A.data(j).value.d(i+1);
    end
    end
    elseif A.data(j).value.d(i) <= Values(2,j)
    Results{j}{k,1} = sectogmt(A.data(j).value.t(i));
    Results{j}{k,2} = A.data(j).value.d(i);
    if i>1
    * if isempty(i-1)
    * i=i-1;
    else
    Results{j}{k,3} = sectogmt(A.data(j).value.t(i-1));
    Results{j}{k,4} = A.data(j).value.d(i-1);
    end
    end
    if i<length(A.data(j).value.d(:))
    * if isempty(i+1)
    * i=i+1;
    else
    Results{j}{k,5} = sectogmt(A.data(j).value.t(i+1));
    Results{j}{k,6} = A.data(j).value.d(i+1);
    end
    end
    end
    k=k+1;
    end
    end

    end
  • ThraxThrax 🐌 Austin, TX Icrontian
    edited August 2009
    I disabled the smiley faces for you.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited August 2009
    I need to see an example of the data you're trying to process.
  • edited August 2009
    Unfortunately I am not able to post the data. I am pulling data from two sources, one has a column of times, represented by the variable "t" and a column of numbers, represented by the variable "d". I know the isempty command is not accurate because the column of numbers sometimes has not only blank lines with no numbers, but some lines have words in them instead of numbers. I want to skip both of those cases, only printing out the previously found number.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited August 2009
    I can't troubleshoot without a simple example. Can you copy/paste like 4 lines into a post (with an example of the 'problem'). Make something up that is formatted the same.
  • edited August 2009
    <table x:str="" style="border-collapse: collapse; width: 225pt;" width="299" border="0" cellpadding="0" cellspacing="0"><col style="width: 136pt;" width="181"> <col style="width: 89pt;" width="118"> <tbody><tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt; width: 136pt;" width="181" height="17">2009_010:10:01:13</td> <td style="width: 89pt;" x:num="" width="118" align="right">67.14</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">2009_010:10:01:14</td> <td>
    </td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">2009_010:10:01:15</td> <td>SYS_LOSS</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">2009_010:10:01:16</td> <td>
    </td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">2009_010:10:01:17</td> <td>
    </td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">2009_010:10:01:18</td> <td x:num="" align="right">235.82</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">2009_010:10:01:19</td> <td>
    </td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">2009_010:10:01:20</td> <td>
    </td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">2009_010:10:01:21</td> <td x:num="" align="right">67.15</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">2009_010:10:01:22</td> <td>
    </td> </tr> </tbody></table>

    So, for example 235.82 is the violation. The spreadsheet needs to print out the time and violation, which I have no problem. The number just before and just after the violation, along with the time also need to be printed to the spreadsheet. The empty cells as well as the SYS_LOSS is creating problems in my code. What I want the final printed spreadsheet to look like is:

    <table x:str="" style="border-collapse: collapse; width: 225pt;" width="299" border="0" cellpadding="0" cellspacing="0"><col style="width: 136pt;" width="181"> <col style="width: 89pt;" width="118"> <tbody><tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt; width: 136pt;" width="181" height="17">2009_010:10:01:13</td> <td style="width: 89pt;" x:num="" width="118" align="right">67.14</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">2009_010:10:01:18</td> <td x:num="" align="right">235.82</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">2009_010:10:01:21</td> <td x:num="" align="right">67.15</td> </tr> </tbody></table>
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited August 2009
    I loaded this in notepad and properly indented it
    k=1;
    for j = 1:6
    	if Checks(j)
    		for i = 1:length(A.data(j).value.d(:))
    			if A.data(j).value.d(i) >= Values(1,j);
    				Results{j}{k,1} = sectogmt(A.data(j).value.t(i));
    				Results{j}{k,2} = A.data(j).value.d(i);
    				if i>1
    					* if isempty(i-1) 
    						* i=i-1; 
    					else
    						Results{j}{k,3} = sectogmt(A.data(j).value.t(i-1));
    						Results{j}{k,4} = A.data(j).value.d(i-1); 
    					end
    				end
    				if i
    					* if isempty(i+1) 
    						* i=i+1;
    					else
    						Results{j}{k,5} = sectogmt(A.data(j).value.t(i+1));
    						Results{j}{k,6} = A.data(j).value.d(i+1); 
    					end 
    				end
    			elseif A.data(j).value.d(i) <= Values(2,j)
    				Results{j}{k,1} = sectogmt(A.data(j).value.t(i));
    				Results{j}{k,2} = A.data(j).value.d(i);
    				if i>1
    					* if isempty(i-1)
    						* i=i-1; 
    					else
    						Results{j}{k,3} = sectogmt(A.data(j).value.t(i-1));
    						Results{j}{k,4} = A.data(j).value.d(i-1); 
    					end
    				end
    				if i
    					* if isempty(i+1)
    						* i=i+1; 
    					else
    						Results{j}{k,5} = sectogmt(A.data(j).value.t(i+1));
    						Results{j}{k,6} = A.data(j).value.d(i+1); 
    					end
    				end
    			end
    		k=k+1;
    		end
    	end
    
    end
    

    1) When you say isempty(i-1), you're just testing if i-1 is empty, not whatever is indexed by i-1.
    2) Same when you say if i. You're just testing i, not whatever is at i.
  • edited August 2009
    I guess I don't understand how to change this. I am obviously not extremely experienced in coding.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited August 2009
    Here's code to do what you requested.
    [time, data] = textread('tmp.txt','%s %s');
    
    index= 1;
    out = {};
    for ii=1:length(time)
        if(str2num(data{ii}))
            out{index} = [time{ii} '    ' data{ii}];
            index = index+1;
        end
    end
    

    out will be a cell array of the data.
  • edited August 2009
    here is the code you gave me, implemented into my code. I am receiving an error when to get to the line: if(str2num(A.data(j).value.d(ii))). Did I place this into my code correctly? I attached the code as a .txt<length(a.data(j).value.d(>
    <length(a.data(j).value.d(></length(a.data(j).value.d(></length(a.data(j).value.d(>
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited August 2009
    what error...

    What's wrong with the code i gave you?
  • edited August 2009
    ??? Error using ==> str2num at 33
    Requires string or character array input.

    Error in ==> KUtemps>LimitCheck at 762
    if(str2num(A.data(j).value.d(ii)))

    Error in ==> KUtemps>Plot_Callback at 685
    LimitCheck(handles);

    Error in ==> gui_mainfcn at 96
    feval(varargin{:});

    Error in ==> KUtemps at 49
    gui_mainfcn(gui_State, varargin{:});

    ??? Error while evaluating uicontrol Callback
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited August 2009
    you never define/assign ii.
  • edited August 2009
    yes I do:

    for ii = 1:length(A.data(j).value.t(:))
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited August 2009
    To be completely honest, I can't understand your code.
  • edited August 2009
    i have attached updated code which has comments guiding you through.
Sign In or Register to comment.