Saving next line to .mat - matlab

I need to save some data to an existing table. So i have column names and one row that has data. Now I get the second set of information and i need to put it in the second row and so on.
Can you just point me where i can find this.
I have done this so far. Played arround with save( -struct) but doesn't seem to work.
if exist('table.mat','file')
...
...
else
dataCell = [name,trez,score];
colNames = {'Name','R','G','B','Shape'};
uisave({'colNames','dataCell'},'table');
end
So I check if there is table.mat, if there is none it creates it with some passed values. Now table.mat exists I need to put the second values without deleting other values.
UPDATE
OK i made the code like this:
if exist('table.mat','file')
dataCell = [name,num2cell(trez),num2cell(score)];
save('table.mat', '-append','dataCell');
else
dataCell=[name,num2cell(trez),num2cell(score)];
colNames={'Name','R','G','B','Shape'};
uisave({'colNames','dataCell'},'table');
end
But when i do save data using :
dataCell = [name,num2cell(trez),num2cell(score)];
save('table.mat', '-append','dataCell');
It deletes the old entry. Lets say in my table information is as it follows :
Name | R | G | B | Shape |
Orange | 239 | 135 | 2 | 0.87
Then if I try to save another entry like :
Apple | 100 |31 |56 | 0.79
It deletes the Orange. So do i need to add something or use some other method for this kind of information saving?

The save command can take an -append flag, which allows you to add data to existing files without overwriting old data. However for .mat files -append only allows you to add new variables. If you specify a variable name that already exists in the .mat file it will be overwritten.
However, if you are saving to an ASCII file then data is simply appended to the end of the file.
This presents you with two options.
Save using a .mat file, but for each variable you want to save you would need to load any variables with the same name from the .mat file, combine old variable with the new variable and then resave it to the file.
Save the matrices using an ASCII format, and then convert the from ASCII when you load the file.
Update: After re-reading your original question, I have to ask why not save in a single operation rather than saving line by line?

Related

How do I extract the last string of a csv file and append it to the other?

I have csv file of many rows, each having 101 columns, with the 101th column being a char, while the rest of the columns are doubles. Eg.
1,-2.2,3 ... 98,99,100,N
I implemented a filter to operate on the numbers and wrote the result in a different file, but now I need to map the last column of my old csv to my new csv. how should I approach this?
I did the original loading using loadcsv but that didn't seem to load the character so how should I proceed?
In MATLAB there are many ways to do it, this answer expands on the use of tables:
Input
test.csv
1,2,5,A
2,3,5,G
5,6,8,C
8,9,7,T
test2.csv
1,2,1.2
2,3,8
5,6,56
8,9,3
Script
t1 = readtable('test.csv'); % Read the csv file
lastcol = t{:,end}; % Extract the last column
t2 = readtable('test2.csv'); % Read the second csv file
t2.addedvar = lastcol; % Add the last column of the first file to the table from the second file
writetable(t2,'test3.csv','Delimiter',',','WriteVariableNames',false) % write the new table in a file
Note that test3.csv is a new file but you could also overwrite test2.csv
'WriteVariableNames',false allows you to write the csv file without the headers of the table.
Output
test3.csv
1,2,1.2,A
2,3,8,G
5,6,56,C
8,9,3,T

Using PowerShell to extract data from a large tab-separated text file, masked it and then merge the masked data back to the original file

I am newbie to Windows PowerShell and wanted to know if is it possible to use PowerShell to extract specific data from tab-delimited(.dat) file and merge it back together to the original file.
The reason behind the extraction of data is that they are sensitive data and requires masking.
Upon extraction, I would require to mask the data and after masking, would require to merge this masked data again back to its original file on their specific places.
Please provide some pointers, any kind of help would be appreciated.
Thank you in advance.
Solution
Here's a solution based on my limited understanding of your question (if you add more details I may be able to be more specific)
Code
Seems all you need to do is read all the data, modify and write it to the file, so here it is!
$Columns = 2,4 # Columns to mask out (Indexes start from 0)
cat ./lol.dat | % {
$arr = $_.split("`t")
$Columns | % {$arr[$_] = '*'*$arr[$_].length}
$arr.join("`t")
} | Out-File ./lol.dat

Octave: create .csv files with varying file names stored in a sub folder

I have multiple arrays with string data. All of them should be exported into a .csv file. The file should be saved in a subfolder. The file name is variable.
I used the code as follows:
fpath = ('./Subfolder/');
m_date = inputdlg('Date of measurement [yyyymmdd_exp]');
m_name = inputdlg('Characteristic name of the expteriment');
fformat = ('.csv');
fullstring = strcat(fpath, m_date,'_', m_name, fformat);
dlmwrite(fullstring,measurement);
However, I get an error that FILE must be a filename string or numeric FID
What's the reason?
Best
Andreas
What you are asking to do is fairly straightforward for Matlab or Octave. The first part is creating a file with a filename that changes. the best way to do this is by concatenating the strings to build the one you want.
You can use: fullstring = strcat('string1','string2')
Or specifically: filenameandpath = strcat('./Subfolder/FixedFileName_',fname)
note that because strings are pretty much just character arrays, you can also just use:
fullstring = ['string1','string2']
now, if you want to create CSV data, you'll first have to read in the file, possibly parse the data in some way, then save it. As Andy mentioned above you may just be able to use dlmwrite to create the output file. We'll need to see a sample of the string data to have an idea whether any more work would need to be done before dlmwrite could handle it.

Matlab publish - Want to use a custom file name to publish several pdf files

I have several data log files (here: 34) for those I have to calculate some certain values. I wrote a seperate function to publish the results of the calculation in a pdf file. But I only can publish one file after another, so it takes a while to publish all 34 files.
Now I want to automize that with a loop - importing the data, calculate the values and publish the results for every log file in a new pdf file. I want 34 pdf files for every log file at the end.
My problem is, that I couldn't find a way to rename the pdf files during publishing. The pdf file is always named after the script which is calculating the values. Obviously the pdf is overwritten within a loop. So at the end everything is calculated, but I only have the pdf from the last calculated log file.
There was this hacky solution to change the Matlab publish script, but since I don't have admin rights I can't use that:
"This is really hacky, but I would modify publish to accept a new option prefix. Replace line 93
[scriptDir,prefix] = fileparts(fullPathToScript);
with
if ~isfield(options, 'prefix')
[scriptDir,prefix] = fileparts(fullPathToScript);
else
[scriptDir,~] = fileparts(fullPathToScript);
prefix = options.prefix; end
Now you can set options.prefix to whatever filename you want. If you want to be really hardcore, make the appropriate modifications to supplyDefaultOptions and checkOptionFields as well."
Any suggestions?
Thanks in advance,
Martin
Here's one idea using movefile to rename the resultant published PDF on each iteration:
for i = 1:34
file = publish(files(i)); % Replace with your own command(s)
[pathStr,fileName,ext] = fileparts(file);
newFile = [pathStr filesep() fileName '_' int2str(i) ext]; % Example: append _# to each
[success,msg,msgid] = movefile(file,newFile);
if ~success
error(msgid,msg);
end
end
Also used are fileparts and filesep. See this question for other ways to rename and move files.

Appending data to a mat file in MATLAB

I have a mat file with some data and i want to add additional data at the end of file whenever a function is called. How can i do it? By save -append my existing data is overwritten. But for me data should not be overwritten. Reply as early as possible.
You've given no information about the type of data you are storing, but I suspect you might be trying to append values to an array which is stored in a file using -append; however, -append only adds new variables to a file. If you save a variable with the same name, it will overwrite it. Instead, just do the append manually:
I'll assume that we are talking about a 1xn vector, you can adjust the concatenation step as necessary.
x = load('myfile');
x = [ x newX ];
save('myfile', 'x');