0

I am fairly new to programming, and started programming in python 3. The data I want to analyze have already been processed in a matlab program, and I need to export them. I don't know anything about matlab, and after searching the web I came up with this:

fileID = fopen('VarA.txt','w');
fprintf(fileID,'%.10f \n',data_1(:,1));
fclose(fileID);

fileID = fopen('varB.txt','w');
fprintf(fileID,'%.10f \n',data_1(:,2));
fclose(fileID);

This gives me 2 .txt files with x and y coordinates respectively. I have about 1000 strings (which each contains about 10k datapoints), so this seems like an awful way to do this.

My question is then; how can I export these datasets with an efficient code? For instance: I am writing a dataset to 2 different .txt files which separates the 2 variables, instead of a hash saved in 1 file.

2
  • This is phrased as a set of statements, not a question. What are you trying to ask? Commented Jan 16, 2016 at 15:28
  • Are you going to install and use numpy and scipy? Or just Python? Commented Jan 16, 2016 at 16:29

1 Answer 1

1

If you are interested in exporting a lot of structured numerical data, i recommend you use the HDF5 functions in matlab to write these, and the corresponding python library to read these.

To simplify the code you showed there, read about dlmwrite in the matlab help.

The way you choose (or via dlmwrite) may sound awful in the beginning, but very often will not really have an impact on the performance.

Sign up to request clarification or add additional context in comments.

3 Comments

Or even simpler, use save with the option -v7.3 which creates a gzip compressed HDF5 file.
If you install the h5py library, you also need numpy. scipy has a loadmat that can read .mat files.
The choice between using a .mat file and an explicitly constructed HDF5 file depends mainly on whether a more specific control over the format is desired. If that is not the case, then using a .mat file (as suggested in both comments) is an appropriate solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.