开发者

Automating inputs to Matlab

I'm helping out with grading in a Matlab course this summer and one of the assignments consists of building a simple user interface for plotting functions and altering their appearance. All the input is done to the command-line, so the menus look something like:

=====================
1. New function
2. Change line-width
3. Change line-style
=====================

I always start out testing the same way when grading to test basic functionality:

1, 2, sin(x) (create a graph of sin(x) in window 2)

1, 3, exp(x) (create a graph of exp(x) in window 3)

2, 3, 2 (change the line-width of plot number 3 to 2)

3, 2, -- (change the line-style of plot number 2 to dashed)

etc.

Since the majority of hand-ins follow the same suggested menu structure it would make my life easier if there was a way to automatically do these inputs with a script instead of repeating the same sequence of numbers and letters over and over. The script itself doesn't have to be in Matlab although开发者_高级运维 I'm guessing it would be practical since I need to check the resulting graphs manually (i.e. I need the input to be entered into the Matlab-command line).

Basically I want Matlab to read the keystrokes "1 [enter] 2 [enter] sin(x) [enter] ..." to the command-line while running another Matlab-script.

Any ideas?


One possibility is to create a private version of the input function that shadows the built-in one. Each time it is called, it returns a value from a predetermined set of values. You can maintain the state using a global/persistent variable (a counter to keep track of how many times the function was called).

Basically you would be creating a method stub to mock the behavior of the real function.


Here is an example to better explain my idea:

/somepath/private/input.m

function ret = input(prompt, varargin)
    %# counter
    persistent count
    if isempty(count), count = 1; end

    %# hard-coded values to return
    values = {
        1 2 'sin(x)' ...
        1 3 'exp(x)' ...
        2 3 2 ...
        3 2 '--' ...
    };

    %# display the prompt message
    disp([prompt '[SIMULATED INPUT #' num2str(count) ']'])

    %# return value
    ret = values{count};

    %# increment counter
    count = count + 1;
    if count>12
        count = 1;
    end
end

/somepath/hw1.m

function hw1
    %# image this is a sample homework file, 
    %# using the standard INPUT function to ask for input from the user
    %# and do something useful with it :)

    num1 = input('enter a number: ');
    num2 = input('enter a second number: ');
    str = input('enter a string: ', 's');
    fprintf('You entered: %d %d %s\n',num1,num2,str);

    num1 = input('enter a number: ');
    num2 = input('enter a second number: ');
    str = input('enter a string: ', 's');
    fprintf('You entered: %d %d %s\n',num1,num2,str);

    num1 = input('enter a number: ');
    num2 = input('enter a second number: ');
    num3 = input('enter a third number: ');
    fprintf('You entered: %d %d %d\n',num1,num2,num3);

    num1 = input('enter a number: ');
    num2 = input('enter a second number: ');
    str = input('enter a string: ', 's');
    fprintf('You entered: %d %d %s\n',num1,num2,str);
end

This is the output you get:

enter a number: [SIMULATED INPUT #1]
enter a second number: [SIMULATED INPUT #2]
enter a string: [SIMULATED INPUT #3]
You entered: 1 2 sin(x)
enter a number: [SIMULATED INPUT #4]
enter a second number: [SIMULATED INPUT #5]
enter a string: [SIMULATED INPUT #6]
You entered: 1 3 exp(x)
enter a number: [SIMULATED INPUT #7]
enter a second number: [SIMULATED INPUT #8]
enter a third number: [SIMULATED INPUT #9]
You entered: 2 3 2
enter a number: [SIMULATED INPUT #10]
enter a second number: [SIMULATED INPUT #11]
enter a string: [SIMULATED INPUT #12]
You entered: 3 2 --


Have a look at AutoIt and see if it suits your needs:

http://www.autoitscript.com/site/autoit/

cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜