Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7088849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:51:04+00:00 2026-05-28T07:51:04+00:00

I am using a script in MatLab that works perfectly fine by itself, but

  • 0

I am using a script in MatLab that works perfectly fine by itself, but I need to make a function out of it.
The script read a .csv file, extract all values, start a timer, and at each tick displays the corresponding coordinates extracted from the .csv, resulting in a 3D animation of my graph.
What I would like is to give it the location of the .csv, so that it starts displaying the graphs for this csv.

Here is what I have come up with:

function handFig(fileLoc)

csv=csvread(fileLoc,1,0);  
both = csv(:,2:19);

ax=axes;
set(ax,'NextPlot','replacechildren');

Dt=0.1; %sampling period in secs

k=1;
hp1=text(both(k,1),both(k,2),both(k,3),'thumb'); %get handle to dot object
hold on;
hp2=text(both(k,4),both(k,5),both(k,6),'index');
hp3=text(both(k,7),both(k,8),both(k,9),'middle');
hp4=text(both(k,10),both(k,11),both(k,12),'ring');
hp5=text(both(k,13),both(k,14),both(k,15),'pinky');
hp6=text(both(k,16),both(k,17),both(k,18),'HAND');
L1=plot3([both(k,1),both(k,16)],[both(k,2),both(k,17)],[both(k,3),both(k,18)]);
L2=plot3([both(k,4),both(k,16)],[both(k,5),both(k,17)],[both(k,6),both(k,18)]);
L3=plot3([both(k,7),both(k,16)],[both(k,8),both(k,17)],[both(k,9),both(k,18)]);
L4=plot3([both(k,10),both(k,16)],[both(k,11),both(k,17)],[both(k,12),both(k,18)]);
L5=plot3([both(k,13),both(k,16)],[both(k,14),both(k,17)],[both(k,15),both(k,18)]);
hold off;

t1=timer('TimerFcn','k=doPlot(hp1,hp2,hp3,hp4,hp5,hp6,L1,L2,L3,L4,L5,both,t1,k)','Period', Dt,'ExecutionMode','fixedRate');
start(t1);

end

And the doplot function used:

function k=doPlot(hp1,hp2,hp3,hp4,hp5,hp6,L1,L2,L3,L4,L5,pos,t1,k)

k=k+1;
if k<5000%length(pos)
set(hp1,'pos',[pos(k,1),pos(k,2),pos(k,3)]);
axis([0 255 0 255 0 255]);
set(hp2,'pos',[pos(k,4),pos(k,5),pos(k,6)]);
set(hp3,'pos',[pos(k,7),pos(k,8),pos(k,9)]);
set(hp4,'pos',[pos(k,10),pos(k,11),pos(k,12)]);
set(hp5,'pos',[pos(k,13),pos(k,14),pos(k,15)]);
set(hp6,'pos',[pos(k,16),pos(k,17),pos(k,18)]);
set(L1,'XData',[pos(k,1),pos(k,16)],'YData',[pos(k,2),pos(k,17)],'ZData',[pos(k,3),pos(k,18)]);
set(L2,'XData',[pos(k,4),pos(k,16)],'YData',[pos(k,5),pos(k,17)],'ZData',[pos(k,6),pos(k,18)]);
set(L3,'XData',[pos(k,7),pos(k,16)],'YData',[pos(k,8),pos(k,17)],'ZData',[pos(k,9),pos(k,18)]);
set(L4,'XData',[pos(k,10),pos(k,16)],'YData',[pos(k,11),pos(k,17)],'ZData',[pos(k,12),pos(k,18)]);
set(L5,'XData',[pos(k,13),pos(k,16)],'YData',[pos(k,14),pos(k,17)],'ZData',[pos(k,15),pos(k,18)]);

else
k=1;
set(hp1,'pos',[pos(k,1),pos(k,2),pos(k,3)]);
axis([0 255 0 255 0 255]);
set(hp2,'pos',[pos(k,4),pos(k,5),pos(k,6)]);
set(hp3,'pos',[pos(k,7),pos(k,8),pos(k,9)]);
set(hp4,'pos',[pos(k,10),pos(k,11),pos(k,12)]);
set(hp5,'pos',[pos(k,13),pos(k,14),pos(k,15)]);
set(hp6,'pos',[pos(k,16),pos(k,17),pos(k,18)]);
end

However, when I run handFig('fileName.csv'), I obtain the same error everytime:

??? Error while evaluating TimerFcn for timer ‘timer-7’

Invalid handle object.

I figured that it might come from the function trying to create a new ‘csv’ and ‘both’ everytime, so I tried removing them, and feeding the function the data directly, without results.

What is exactly the problem? Is there a solution?

Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:51:05+00:00Added an answer on May 28, 2026 at 7:51 am

    I think it’s because when you call doPlot in the timer for the first time, you pass in t1 as an argument, and it might not exist the first time.

    Does doPlot need t1 at all? I’d suggest modifying it so it’s not used, and then your call to:

    t1=timer('TimerFcn','k=doPlot(hp1,hp2,hp3,hp4,hp5,hp6,L1,L2,L3,L4,L5,both,k)','Period', Dt,'ExecutionMode','fixedRate');
    

    Note the missing t1 in the doPlot call.

    Either that, or initialise your t1 before you create the timer so it has some value to pass in.


    Update (as an aside, can you use pause(Dct) in a loop instead? seems easier)

    Actually, now I think it’s a problem of scope.
    It took a bit of digging to get to this, but looking at the Matlab documentation for function callbacks, it says:

    When MATLAB evaluates function handles, the same variables are in scope as when the function handle was created. (In contrast, callbacks specified as strings are evaluated in the base workspace.)

    You currently give your TimerFcn argument as a string, so k=doPlot(...) is evaluated in the base workspace. If you were to go to the matlab prompt, run handFig, and then type h1, you’d get an error because h1 is not available in the global workspace — it’s hidden inside handFig.

    That’s the problem you’re running into.

    However, the workaround is to specify your function as a function handle rather than a string (it says function handles are evaluated in the scope in which they are created, ie within handFig).

    Function handles to TimerFcn have to have two arguments obj and event (see Creating Callback Functions). Also, that help file says you have to put doPlot in its own m-file to have it not evaluate in the base Matlab workspace.

    In addition to these two required input arguments, your callback
    function can accept application-specific arguments. To receive these
    input arguments, you must use a cell array when specifying the name of
    the function as the value of a callback property. For more
    information, see Specifying the Value of Callback Function Properties.

    It goes through an example of what you have to do to get this working. Something like:

    % create timer
    t = timer('Period', Dt,'ExecutionMode','fixedRate');
    % attach `k` to t so it can be accessed within doPlot
    set(t,'UserData',k);
    % specify TimerFcn and its extra arguments:
    t.TimerFcn = { @doPlot, hp1, hp2, hp3, ...., both };
    start(t)
    

    Note — the reason k is set in UserData is because it needs to be somehow saved and modified between calls to doPlot.

    Then modify your doPlot to have two arguments at the beginning (which aren’t used), and not accept the k argument. To extract k you do get(timer_obj,'UserData') from within doPlot:

    function k=doPlot(timer_obj, event, hp1,hp2,hp3,.....)
        k = get(timer_obj,'UserData');
        .... % rest of code here.
        % save back k so it's changed for next time!
        set(timer_obj,'UserData',k); 
    

    I think that’s on the right track – play around with it. I’d highly recommend the mathworks forums for this sort of thing too, those people are whizzes.

    This thread from the mathworks forum was what got me started and might prove helpful to you.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a script that is sending out emails and I am receiving
Everything seems fine when using script as #/bin/sh on ubuntu but now on using
I'm using script language ChaiScript with c++ and Qt. I've defined such function: void
I'm using this script to display all the images in a folder, but I
I'm using a script that connects to a cluster through ssh and sends some
I've written the following function for importing excel files into matlab. The function works
I've written a script that saves its output to a CSV file for later
I know that there is the possibility of Matlab of pausing a Matlab function
i am using script manager to call a web method on client side but
Without using a WebBrowser control, how could I execute a JavaScript function, that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.