I have ground reaction force csv files which are 501x11 / 11x501 double
Female_GRF = csvread('Female_Fz.csv');
Male_GRF = csvread('Male_Fz.csv');
Female_GRF = Female_GRF';
Male_GRF = Male_GRF';
Please can anyone direct me how I can resample or downsample these from 501 nodes to 101x11 / 11x101?
Any help is appreciated.
Thank you
Matlab has a builtin downsampling method:
>> Female_GRF = downsample(Female_GRF,5);
replaces Female_GRF with the downsampled version.
On the other hand, if by "downsample", you mean keep every 5th entry, then that's easier: the command
>> Female_GRF = Female_GRF(1:5:end);
replaces the variable Female_GRF with a "downsampled" version.
Related
I wish to import a large number of csv files to MATLAB. I can do this without any difficulty except it takes a lot of time - about 3 seconds per file with the following code. Is there a way to do it faster? Here A is a matrix with 15 rows and 250 columns. There are 150 files.
tic
file_name = [];
for w = scenario_size:-1:1
file_name = sprintf('monthly_population_%d.csv',w) ; % read file name f
A = xlsread(file_name);
pop(:,:,w) = A' ;
end
clear A
toc
You may have improved performance by using readmatrix, instead of xlsread. For example:
A = readmatrix(file_name);
Or, if you are on a Matlab release which doesn't have readmatrix, try readtable:
A = table2array(readtable(file_name));
I have made a datafile from OpenFoam that extracts velocity at a certain location in time. I would like to extract two of these velocity and take there time average. For example I would like to extract the numbers: 0.0539764,0.0104665,0.00201741 and so on from probe 0. And extract the numbers: 0.690374, 0.711402, 0.699848 and so on from probe 1. How can this be done in Matlab?
I have done something similar before, but then the probes only consisted of 1 number (without the parentheses), now it consist of 3 numbers inscribed in a parentheses, I don't know what I am supposed to do.
Help is much appreciated.
Link to the whole file: https://drive.google.com/file/d/0B9CEsYCSSZUSdjFzYXVFc1RhM0k/view?usp=sharing
This will create two matrices probe0 & probe1. You can index just the first column of each if that is all you are after.
id = fopen('testprobe.txt','r');
t = textscan(id,'%s','delimiter',sprintf('\n'));
fclose(id);
out = regexp(t{1,1}(6:end-3), '(?<=\()[^)]*(?=\))', 'match', 'all');
probe0 = zeros(size(out,1),3);
probe1 = zeros(size(out,1),3);
for i = 1:size(out,1)
if ~isempty(out{i,:})
probe0(i,:) = (str2double(split(out{i,1}{1,1})))';
probe1(i,:) = (str2double(split(out{i,1}{1,2})))';
else
probe0(i,:) = [0,0,0];
probe1(i,:) = [0,0,0];
end
end
I am trying to put a legend in Matlab figures that include a symbol in Latex. When I plot the figure, the legend looks fine. However, when I export the figure as a PDF, the legend gets spaces put into it. I don't know why this is happening. Sample code is as follows:
set(groot,'defaultLineLineWidth',2,'defaultAxesFontSize',...
12,'defaultAxesFontName','timesnewroman',...
'defaulttextinterpreter','latex')
x0 = 8;
y0 = 5;
width = 5;
height = 4;
kappa1 = 0.1;
kappa2 = 0.5;
f = linspace(0,2*pi,1000);
y1 = sin(f+kappa1*f.^2);
y2 = sin(f+kappa2*f.^2);
figure(1)
hold on
plot(f,y1,'k')
plot(f,y2,'b')
xlabel('Frequency (MHz)')
ylabel('Amplitude')
legend(strcat('\kappa = 0.1 MHz/','\mu','s'),...
strcat('\kappa = 0.5 MHz/','\mu','s'))
grid on;
set(gcf,'units','inches','Position',[x0,y0,width,height],...
'PaperPositionMode','Auto','PaperUnits','Inches',...
'PaperSize',[width, height]);
saveas(gcf,'legendtest.pdf')
It seems like the error happens when I save the file as a PDF. It saves as a JPG just fine. Below are the two images I get. The jpg is:
But the PDF I get is:
I am using Matlab version R2017a on a Mac running OS 10.12.5. Thanks in advance for any help!
This is a known bug. See the bug report according to which it affects the versions from R2014b to R2017a. A workaround is suggested in that bug report as well which is to generate the pdf file by setting the Renderer to opengl i.e.
set(gcf,'Renderer','opengl');
But then the generated pdf file contains a pixel graphic instead of a vector graphic.
Better thing would be to use the respective unicode values which will produce a vector graphic. i.e.
legend([char(954), ' = 0.1 MHz/',char(956),'s'],...
[char(954), ' = 15 MHz/',char(956),'s']); %char(954) equals 'κ', char(956) equals 'μ'
If you want to use italic characters, it is also possible with unicode characters.
legend([char([55349,57093]), ' = 0.1 MHz/',char([55349,57095]),'s'],...
[char([55349,57093]), ' = 15 MHz/',char([55349,57095]),'s']);
%char([55349,57093]) equals '𝜅' (italic κ), char([55349,57095]) equals '𝜇' (italic μ)
Another workaround is to just interpret the whole legend text with Latex:
h = legend(strcat('$\kappa$ = 0.1 MHz/','$\mu$','s'),...
strcat('$\kappa$ = 0.5 MHz/','$\mu$','s'))
set(h,'Interpreter','latex')
It requires some basic Latex knowledge, e.g. you have to wrap all math signs (kappa, mu) with $ and beware if you want to use any special non-english characters. Changes the look of the legend a bit, but arguably for the better.
Btw, you can skip strcat, it does not serve any purpose.
h = legend('$\kappa$ = 0.1 MHz/$\mu$s',...
'$\kappa$ = 0.5 MHz/$\mu$s')
set(h,'Interpreter','latex')
Works just as well, the same goes for the non latex version.
I have this file which is a series of x, y, z coordinates of over 34 million particles and I am reading them in as follows:
parfor i = 1:Ntot
x0(i,1)=fread(fid, 1, 'real*8')';
y0(i,1)=fread(fid, 1, 'real*8')';
z0(i,1)=fread(fid, 1, 'real*8')';
end
Is there a way to read this in without doing a loop? It would greatly speed up the read in. I just want three vectors with x,y,z. I just want to speed up the read in process. Thanks. Other suggestions welcomed.
I do not have a machine with Matlab and I don't have your file to test either but I think coordinates = fread (fid, [3, Ntot], 'real*8') should work fine.
Maybe fread is the function you are looking for.
You're right. Reading data in larger batches is usually a key part of speeding up file reads. Another part is pre-allocating the destination variable zeros, for example, a zeros call.
I would do something like this:
%Pre-allocate
x0 = zeros(Ntot,1);
y0 = zeros(Ntot,1);
z0 = zeros(Ntot,1);
%Define a desired batch size. make this as large as you can, given available memory.
batchSize = 10000;
%Use while to step through file
indexCurrent = 1; %indexCurrent is the next element which will be read
while indexCurrent <= Ntot
%At the end of the file, we may need to read less than batchSize
currentBatch = min(batchSize, Ntot-indexCurrent+1);
%Load a batch of data
tmpLoaded = fread(fid, currentBatch*3, 'read*8')';
%Deal the fread data into the desired three variables
x0(indexCurrent + (0:(currentBatch-1))) = tmpLoaded(1:3:end);
y0(indexCurrent + (0:(currentBatch-1))) = tmpLoaded(2:3:end);
z0(indexCurrent + (0:(currentBatch-1))) = tmpLoaded(3:3:end);
%Update index variable
indexCurrent = indexCurrent + batchSize;
end
Of course, make sure you test, as I have not. I'm always suspicious of off-by-one errors in this sort of work.
I'm not sure if anyone can help with this question but here we go. I have 4 folders where each folder contains data for different locations, within the folders I have 8 .txt files which represent the measured variables at each location (i.e. same variables measured in each location). I'm trying to import these into matlab and list the measured variables in astructure so they can be compared and plotted against one another afterwards (without doing this they will over write one another).
I've written a script for importing these into matlab, the script works but not exactly in the way I want it to, the script is as follows:
clear all
pathName = 'E:\University\CEH Lancaster\Project\LA practice\final files';
FolderListing = dir(pathName);
FolderListing = FolderListing(3:end);
%lists the folder in the directory specified by pathName
for i = 1:length(FolderListing);
LName{i} = (FolderListing(i).name);
%obtains the name of each folder
end
for i = 1:length(LName)
TopFolder{i} = fullfile(pathName,LName{i});
%path for each individual folder
dirListing{i} = dir(fullfile(TopFolder{i},'*.txt'));
%list of the .txt files
for ii = 1:length(dirListing{1,1});
fileToRead1{1,i}{ii,1} = (dirListing{1,i}(ii,1).name);
%name of the .txt files in the TopFolder
end
end
for i = 1:length(fileToRead1);
for ii = 1:length(fileToRead1{1});
fid{1,i}{ii,1} = fopen((fullfile(TopFolder{1,i},fileToRead1{1,i}{ii,1})));
%open the files specified by fileToRead prior to importing the data
%into matlab
data{1,i}{ii,1} = textscan(fid{1,i}{ii,1},'%f');
%import the data into matlab
[~,name{1,i}{ii,1}] = fileparts(fileToRead1{1,i}{ii,1});
%obtain the name of each of the variables
Location.(LName{i}).(genvarname(name{1,i}{ii,1})) = data{1,i}{ii,1};
%create a strucutre for the individual locations and the
%variables.
end
end
The problem lies in the final outcome where instead of having Location.Name and then the list of variables, I have Location.Name.variables, which doesn't seem necessary. I realise that its due to the way I've written the last line of the script but I can't seem to change it without it producing an error. Any advice you could give on the problem or on the script in general would be much appreciated.
I think cell2mat is the function you want for this purpose. Here's my usage, see if it fits your needs:
tt = {ones(1,100)};
tt
tt =
[1x100 double]
cell2mat(tt)
ans =
Columns 1 through 15
1 1 1 1 1 1 1 1 1 1 1 1...