• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

MATLAB - basic questions

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.
1 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: http://www.me.pdx.edu/~gerry/MATLAB/programming/basics.html. 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.
slaviknAuthor Commented:

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now