How to request+input variable name+value then write to file?

I am trying to write a script that:
asks user for matrix name %like mat1
asks user for matrix value %like [20,30]
writes the matrix to a file
every time the script is run I want new data added to file, NOT overwriting what's already there.
-------
I use code like this to ask for variable value but don't know how to ask for variable name.
mat1 = input('Enter Name: \n','s');
-------
I use code like this to write to file but I don't know how to write a variable whose name I do not know prior to user putting it in. I also don't know how to make it so it doesn't over write information already present in file.
fid = fopen('locations.dat', 'w');
fprintf(fid,'%s\n',loc1M);
fclose(fid);

 采纳的回答

To append: Open the file with 'a' instead of 'w'.
You do not need to write a matrix whose name you do not know. In your problem description, the matrix name can be treated as a pure string to be written at output time: you never need to store anything into a variable with that name. Something like
fprintf(fid, '%s\n', matrix_name);
fprintf(fid, '%f ', matrix_value);
except that you need to adjust the writing of matrix value to get the correct number of columns across.

3 个评论

Thanks for the append tip.
However I do no need to write a matrix who's name and value I do not know (unless I misunderstood something).
This is a script for a user who will not program; ie will not use any functions of any kind.
-User runs the script
-Gets a prompt for location name
-Inputs location name
-Gets prompt for latitude and longitude
-Inputs latitude and longitude
-Script writes the matrix name and values now stored in temp memory to locations.dat file thus building a database.
I do not know what location the user will choose on the surface of the planet nor what the user chooses to name it so I need the script to prompt for all that information and then once it has it write it to file.
Ideally I would have a 1x3 matrix and have [name,lat,long] however apparently you cant have a string and a numeric variable in the same matrix.
After I figure this out the next problem will be a function to scan the file for one of the names it holds and recall the latitude and longitude values to perform calculations using them. Not sure yet how I will accomplish that either.
I figured out how to do this by creating a new file for each location but I want to accomplish this using one file as a database :(
fprintf(fid, '%s %f %f\n', location_name, latitude, longitude);
THANKS! That seems to work great except it writes
|.wSANJOSE 20.000000 30.000000
plus a little arrow character between "." and "w" that I cant paste in to this window. Any way to get rid of that "|.w" gibberish?
Could you recommend a command to search the file for name and import the adjacent variables (latitude/longitude)?
thanks!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Data Import and Analysis 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by