MATLAB - basic questions

Posted on 2004-10-29
Last Modified: 2012-06-27
Hi all,

1) How do I print a formatted string to screen? sprintf also prints "ans = ...". fprintf works perfectly well, but this doesn't seem to be the most elegant way, because this is used for writing to files (and I cannot specify print to stdout, like in C).

2) How do I pass parameters by reference to a function?

3) When I write "load f1.txt", "load f2.txt" etc, the matrixes are
loaded into f1, f2 etc. Can I load it to a certain variable NOT with the name of the file? (I want it to be loaded to "f", for example).Of course I can read into f1, and then just copy into f, and then clear f1, but I have hundreds of files, and this will take extra time.
Something else that could solve this is to define a pointer. I.e. load the file into f1, and then assign f to be a pointer to f. I prefer the first way (difect reading to "f"), but if it's impossible, a pointer will also do...

Please help.
Question by:slavikn
    LVL 9

    Accepted Solution

    I don't have access to MATLAB, so I'm using Octave, which is supposed to be a fairly good clone. Its documentation is fairly weak, however.

    For (1), you could try wrapping the sprintf output using disp: disp(sprintf(....)). That seems to work OK for me.

    Octave documentation states that all function calls are by value, and that you cannot call a function by reference. However, in googling around a bit, I stumbled onto this page: On it, there is this warning in bold letters: "Unlike other languages, the variables in the input_parameter_list should never be altered by the statements inside the function." That suggests to me that MATLAB passes arguments by reference.

    For (3), Octave also has a load function, and it seems to work the same way that you describe. If you insert appropriate comments at the beginning of the file, you can control the name of the variable to which the data is assigned. Here's an example:

    # name: m
    # type: matrix
    # rows: 1
    # columns: 5
     1 0 1 0 1

    When you load such a file, the array data will be assigned to the variable, "m." Perhaps you can add such comments when you create your files.

    Hope this helps. Maybe somebody who actually uses MATLAB can provide some better answers.
    LVL 1

    Author Comment


    1) Indeed, disp(sprintf(...)) works. Thanks.
    2) No. Variables aren't passed by reference.
    3) I've found a way: whatever = load('filename');

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    How to Win a Jar of Candy Corn: A Scientific Approach! I love mathematics. If you love mathematics also, you may enjoy this tip on how to use math to win your own jar of candy corn and to impress your friends. As I said, I love math, but I gu…
    This article seeks to propel the full implementation of geothermal power plants in Mexico as a renewable energy source.
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now