-3

The filename in question is a MAT file that contains elements in the form of "a - bi" where 'i' signifies an imaginary number. The objective is to separate the real, a, and imaginary, b, parts of these elements and put them into two arrays. Afterwards, a text file with the same name as the MAT file will be created to store the data of the newly created arrays.

Code:

function separate(filename)

    realArray = real(filename)
    imagArray = imag(filename)

    fileIDname = strcat(filename, '.txt')
    fileID = fopen(fileIDname, 'w')

    % more code here - omitted for readability

end

I am trying to run the above code via command window. Here's what I've tried so far:

%attempt 1
separate testFileName 

This does not work as the output does not contain the correct data from the MAT file. Instead, realArray and imagArray contains data based on the ascii characters of "testFileName".

e.g. first element of realArray corresponds to the integer value of 't', the second - 'e', third - 's', etc. So the array contains only the number of elements as the number of characters in the file name (12 in this case) instead of what is actually in the MAT file.

%attempt 2
load testFileName
separate(testFileName)

So I tried to load the testFileName MAT variable first. However this throws an error:

Complex values cannot be converted to chars
Error in strcat (line 87)
    s(1:pos) = str;
Error in separate (line xx)
   fileIDname = strcat(filename, '.txt')

Basically, you cannot concatenate the elements of an array to '.txt' (of course). But I am trying to concatenate the name of the MAT file to '.txt'.

So either I get the wrong output or I manage to successfully separate the elements but cannot save to a text file of the same name after I do so (an important feature to make this function re-usable for multiple MAT files).

Any ideas?

Steve Cho
  • 431
  • 2
  • 5
  • 10
  • to read and write .mat files, use `save` and `load`. In your first example weirdly you are not even loading anything, so clearly nothign is loaded in consecuence. Also what do you expect `real(fieldname)` to do? Because it grabs the real part of the string that is the fieldname, but I am quite sure you are not expecting that. – Ander Biguri Oct 03 '18 at 15:46
  • @AnderBiguri I am not trying to save my workspace to a MAT file. I am trying to save data into a textfile based on a MAT file of the same name given by the user from the command window. edit: I expect "real(fieldname)" to take the real parts of the elements in the MAT file given by the user. I understand that the 1st example does not work as intended but included it in the question to show my (incorrect) thought process – Steve Cho Oct 03 '18 at 15:50
  • Its hard for me to try to really guess your attempts because they are so off. You need to call `data=load(filename)`, then do whatever you need to `data` and then save it in a text file using basic commands. What is worng with this approach? – Ander Biguri Oct 03 '18 at 15:53
  • Your first example just shows your lack of documentation reading. The usage of `real` and `imag` is so creative, but a glance of the documentation would show you how worng it is. – Ander Biguri Oct 03 '18 at 15:54
  • When you ask "How do you call a function ...?", do you mean what should be the name of the function, or do you mean how do you run the function? – Cris Luengo Oct 03 '18 at 21:08
  • @CrisLuengo I included that part in the title because I wasn't sure if there was a better way of calling the function than the two ways I listed (attempt 1 and attempt 2). Perhaps someone knows something that the MATLAB documentation doesn't explicitly state on their page for calling functions. To answer your question - the latter. – Steve Cho Oct 04 '18 at 12:15

1 Answers1

0

A function to read complex data, modify it and save it in a txt file with the same name would look approximately like:

function dosomestuff(fname)
   % load
   data=load(fname);

   % get your data, you need to knwo the variable names, lets assume its call "datacomplex"
   data.datacomplex=data.datacomplex+sqrt(-2); % "modify the data"

   % create txt and write it.
   fid=fopen([fname,'.txt'],'w');
   fprintf(fid, '%f%+fj\n', real(data.datacomplex), imag(data.datacomplex));
   fclose(fid);

There are quite few assumptions on the data and format, but can't do more without extra information.

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • the variable name would be different depending on the MAT file the user chooses. for example, if the fname is "exampleName", then the variable name would be "exampleName". if the user decides to use this function on the fname "secondName", then the variable name would be "secondName". The code you posted appears to be hard-coded (e.g. "data.datacomplex"). Can this task be soft-coded? – Steve Cho Oct 03 '18 at 16:04
  • You shouldn't want the variable name to be changeable, it's bad practise, hard to maintain, and hard to use (as demonstrated by this issue!). Save the variable with a consistent name, it will save a lot of issues and make your code objectively better. – Wolfie Oct 03 '18 at 16:23
  • @Wolfie it may be bad practice but I do not have the flexibility to set the variable name. MAT files are given from a read-only source. – Steve Cho Oct 03 '18 at 16:56
  • 3
    Is there always one variable in the file? you can just use the `fieldnames` property of the `data` struct which Ander has shown you how to construct to interact with the data, using `data.(name)`, where you've established the `name` string. – Wolfie Oct 03 '18 at 17:05
  • @Wolfie Yes there is always one variable in the file (with an X number of elements). The field name is established based on the MAT file name. Unless MATLAB has a way of universally accessing the first (and in this case, the only) field name of a struct, I cannot use the `data.(name)` if `name` changes depending on the file. – Steve Cho Oct 04 '18 at 12:10
  • Yes you can. As I said, use `fieldnames` to determine the name, regardless and independent of the file name. – Wolfie Oct 04 '18 at 12:35