read value file in Matlab - arrays

Currently I am using read comma-separated value (CSV) file which is csvread in my code. what if I want to read value row by row, which my value is not seperated by comma?
This is my code for save data:
%% Save data to .txt
fid=fopen('MyFile1.txt','w');
fprintf(fid, '%f \n', AngleValue');
fclose(fid);
This is the save values
Then I run a code using csvread
I = csvread('MyFile1.txt');
but I got an error. can anyone give an idea, probably I should change how I save my data or how can I read the data row by row. Thanks

There are other alternatives to csvread. Try dlmread instead
I = dlmread('MyFile.txt');
A more general approach,
Why are you writing AngleValue as text files? Why not use save and load in a more Matlab-ish fashion? read and write in binary format, tailored for Matlab use?
save('MyFile.mat','AngleValue'); %// save to binary Mat file
Once you saved the variable, you can read it:
load('MyFile.mat'); %// that's it!

use
A = fscanf(fileID,formatSpec)
more info

Problem solved. I found out that i just have to put coma (,) when i fprintf my saved data.
My previous code:
fprintf(fid, '%f \n', AngleValue');
My corrected code:
fprintf(fid, '%f,', AngleValue');
So now i can use csvread in my program.

Related

How to export many variables of a simulation result to csv?

I'm trying to export my results from Dymola to excel through a csv file. But I have many results. How can I write them in an array??
I tried to create a for loop but I lack the knowledge on how to type the code.
if (time) <= 0 then
Modelica.Utilities.Files.removeFile("tube_0.02"+".csv");
Modelica.Utilities.Streams.print("temps," +
"delta_fr1,"+
"delta_fr2,"+
"delta_fr3,",
"tube_0.02"+".csv");
else
Modelica.Utilities.Streams.print(String(time) +
"," + String( my_code_tube1[1].delta_fr)+
"," + String( my_code_tube1[2].delta_fr)+
"," + String( my_code_tube1[3].delta_fr),
"tube_0.02"+".csv");
end if;
Instead of having to write delta_fr1, delta_fr2... and then my_code_tube1[1].delta_fr, my_code_tube1[2].delta_fr.... I need to create a for loop because I will have almost 1500 variable to export.
Trying to write the results to a csv file during simulation comes with some troubles in Modelica:
For which time steps do you want to write the result? Using the solver step size is not possible.
But we can define a periodic output using the sample() function
Calling print is quite expensive. You don't want to do that too many times while your simulation runs
The print function always adds a newline after every call. That requires us to write every line at once (but due to the performance limits mentioned before we should write as much text as possible at once anyhow).
Unfortunately, the default maximum string size of Dymola is quite small, limited on 500.
You have to ensure that the current Dymola instance is using an appropriate large value by setting e.g. Advanced.MaxStringLength = 50000.
With these things in mind, we could come up with a code like below:
model SO_print_for
My_code_tube my_code_tube1[1500];
protected
String line;
String f="tube_0.02" + ".csv";
model My_code_tube
Real delta_fr=1;
end My_code_tube;
initial algorithm
line := "temps";
for i in 1:size(my_code_tube1,1) loop
line := line + ", delta_fr" + String(i);
end for;
Modelica.Utilities.Files.removeFile(f);
Modelica.Utilities.Streams.print(line, f);
algorithm
when sample(0, 0.1) then
line := String(time);
for i in 1:size(my_code_tube1,1) loop
line := line + ", "+String(my_code_tube1[i].delta_fr);
end for;
Modelica.Utilities.Streams.print(line, f);
end when;
end SO_print_for;
The code works, but it will slow down your simulation, as many time events are generated (from the sample() function).
Instead of writing a csv during the simulation, you should consider one of the following ways to convert the result file after the simulation has finished:
Use Export Result in the Dymola variable browser to export all or only the plotted variables into csv. This must be done manually after every simulation and can not be scrippted
Use the function DataFiles.convertMATtoCSV. The following two lines of code will extract 1500 delta_fr variables from the .mat result file
vars = {"my_code_tube1["+String(i)+"].delta_fr" for i in 1:1500}
DataFiles.convertMATtoCSV("your-model.mat", vars, "out.csv")
Use Matlab, Octave or FreeMat to open the .mat result file and convert it to a file format which Excel understands.
Dymola provides dymload.m to import .mat result files into Matlab. See the User manual volume 1 of Dymola for details
Use python to convert the results to a csv file, e.g. by using the package DyMat or SDF-Python, which can both be used to read the result files

Create 2d array from csv file in python 3

I have a .csv file which contains data like the following example
A,B,C,D,E,F,
1,-1.978,7.676,7.676,7.676,0,
2,-2.028,6.081,6.081,6.081,1,
3,-1.991,6.142,6.142,6.142,1,
4,-1.990,65.210,65.210,65.210,5,
5,-2.018,8.212,8.212,8.212,5,
6,54.733,32.545,32.545,32.545,6,
..and so on
The format is constant.
I want to load the file in a variable "log" and access them as log[row][column]
example
log[0][2] should give C
log[3][1] should give -1
If use this code
file = open('log.csv')
log = list(file)
when i use this code i get only one dimensional. log[row]
Is their any way to directly store them?
for example
read the file
split with '\n'
split again with ','
Thanks!!
Try this
log = [line.split(',') for line in open('log.csv')]

open text file, modify text, place into sql database with groovy

I have a text file that has a large grouping of numbers (137mb text file) and am looking to use groovy to open the text file, read it line-by-line, modify the numbers, and then place them into a database (as strings). There are going to be 2 items per line that need to be written to separate database columns, which are related.
My text file looks as such:
A.12345
A.14553
A.26343
B.23524
C.43633
C.23525
So the flow would be:
Step 1.The file is opened
Step 2.Line 1 is red
Step 3.Line 1 is split into letter/number pair [:]
Step 4.The number is divided by 10
Step 5.Letter is written to letter data base (as string)
Step 6.Number is written to number database (as string)
Step 7.Letter:number pair is also written to a separate comma separated text file.
Step 8.Proceed to next line (line 2)
Output text file should look like this:
A,1234.5
A,1455.3
A,2634.3
B,2352.4
C,4363.3
C,2352.5
Database for numbers should look like this:
1:1234.5
2:1455.3
3:2634.3
4:2352.4
5:4363.3
6:2352.5
*lead numbers are database index locations, for relational purpose
Database for letters should look like this:
1:A
2:A
3:A
4:B
5:C
6:C
*lead numbers are database index locations, for relational purpose
I have been able to do most of this; the issue I am running into is not be able to use the .eachLine( line -> ) function correctly... and have NO clue how to output the values to the databases.
There is one more thing I am quite dense about, and that is the instance where the script encounters an error. The text file has TONS of entries (around 9000000) so I am wondering if there is a way to make it so if the script fails or anything happens that I can restart the script from the last modified line.
Meaning, the script has an error (my computer gets shut down somehow) and stops running at line 125122 (completes modification of line 125122) of the text file... how do I make it so when I start the script the second time run the script at line 125123.
Here is my sample code so far:
//openfile
myFile = new File("C:\\file.txt")
//set fileline to target
printFileLine = { it }
//set target to argument
numArg = myFile.eachLine( printFileLine )
//set argument to array split at "."
numArray = numArg.split(".")
//set int array for numbers after the first char, which is a letter
def intArray = numArray[2] { it as int } as int
//set string array for numbers after the first char, which is a letter
def letArray = numArray[1] { it as string }
//No clue how to write to a database or file... or do the persistence thing.
Any help would be appreciated.
I would use a loop to cycle over every line within the text file, I would also use Java methods for manipulating strings.
def file = new File('C:\\file.txt')
StringBuilder sb = new StringBuilder();
file.eachLine { line ->
//set StringBuilder to new line
sb.setLength(0);
sb.append(line);
//format string
sb.setCharAt(1, ',');
sb.insert(5, '.');
}
You could then write each line to a new text file, example here. You could use a simple counter (e.g. counter = 0; and then counter++;) to store the latest line that has been read/written and use that if an error occurs. You could catch possible errors within a try/catch statement if you are regularly getting crashes also.
This guide should give you a good start with working with a database (presuming SQL).
Warning, all of this code is untested and should hopefully give you more direction. There are probably many other ways to solve this differently, so keep an open mind.

Using dlmwrite to write cell objects MATLAB

I have a cell array with 7 columns. All these columns contain strings. I want to write this cell array into a text file. To start, I was doing this on only 1 element of the cell and this is my code:
dlmwrite('735.txt',cell{1},'delimiter','%s\t');
cell{1} looks like this:
Columns 1 through 2
[1x30 char] [1x20 char]
Column 3
'Acaryochloris'
Column 4
'Cyanobacteria001'
Columns 5 through 6
'Cyanobacteria00' 'Cyanobacteria'
Column 7
'Bacteria'
It gives me the output without separating the columns. Sample output is:
Acaryochloris_marina_MBIC11017AcaryochlorismarinaAcaryochlorisCyanobacteria001Cyanobacteria00CyanobacteriaBacteria
The correct output should have spaces between all the columns :
Acaryochloris_marina_MBIC11017 Acaryochloris_marina Acaryochloris Cyanobacteria001 Cyanobacteria00 Cyanobacteria Bacteria
Note that for the second column, we need to add the underscore between Acaryochloris and marina. There is originally a space between those two words.
I hope I explained the problem correctly, Would appreciate the help. Thanks!
DLMWRITE is for numerical data. In your case it process the char data as numbers, each character as a time. You probably view the resulted file in such a way that you don't see tab delimiters.
You can use XLSWRITE to write cell string array to a file. If you don't want the output to be in Excel format, run DLMWRITE before it to write some number to a file.
dlmwrite(filename,1)
xlswrite(filename, Acell{1})
Don't call you variable cell, which a keyword in MATLAB.
As an alternative you can write to a file with lower level function, like FPRINTF.
UPDATE:
If you want to use XLSWRITE in a for-loop and not to overwrite the data you can specify the row to start from:
dlmwrite(filename,1)
for k = 1:10
xlswrite( filename, Acell{k}, 1, sprintf('A%d',k) )
end
UPDATE 2:
Unfortunately it does not work anymore in the latest MATLAB releases (I believe starting from R2012b). XLSWRITE gives error about wrong file type.
Something along the lines of the following should do what you want:
fid = fopen('735.txt', 'w');
fprintf(fid, '%s\t', cell{1}{:});
fclose(fid);

J2ME Writing in CSV file through file Connection API

I am creating an application in which i need to add a new column in the csv file and then entries for that particular column.
And I have tried OutputStream and PrintStream but the problem is that the data is being written in starting of the file but i want the data at random position according to my need.
And RandomAccessFile is not identified by the application.
For e.g.
My CSV is:
Name,any_date
A,
B,
c,
And after writing it will look like
Name,any_date
A,p
B,a
C,p
I am using file Connection API to read and write.Can anyone suggest me how to do that??
Thanks in advance.
i think you want to append data in the file.
you can try this
os = fconn.openOutputStream(fconn.fileSize());
os.write(data.getBytes());
This is a simple example to append data at last.

Resources