Skip to content
Snippets Groups Projects
Commit 29100d5b authored by brinn's avatar brinn
Browse files

add: conversion class from csv array strings to float[]. Used by Nicola in...

add: conversion class from csv array strings to float[]. Used by Nicola in Matlab to quickly convert the peak data from the metabolomics database as the native Matlab solution has performance problems.

SVN: 11776
parent 12bff9a9
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2009 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A class featuring a conversion of CSV strings to float arrays. We keep it in the default package
* even when this is deprecated because it is used from Matlab and not having to type any package
* name is preferred by Matlab users.
*
* @author Bernd Rinn
*/
public class Convert
{
private static int countMatches(String str, String sub)
{
if (str == null || str.length() == 0 || sub == null || sub.length() == 0)
{
return 0;
}
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != -1)
{
count++;
idx += sub.length();
}
return count;
}
/**
* Converts the given <var>csvList</var> string, which is expected to contain a comma-separated
* list of values, into a <code>float[]</code>.
*/
public static float[] csvToFloatArray(String csvList)
{
final float[] floatArr = new float[countMatches(csvList, ",") + 1];
int idxStart = 0;
int idxEnd = 0;
int slen = csvList.length();
for (int i = 0; i < floatArr.length; ++i)
{
while (idxEnd < slen && csvList.charAt(idxEnd) != ',')
{
++idxEnd;
}
floatArr[i] = Float.parseFloat(csvList.substring(idxStart, idxEnd));
idxStart = idxEnd + 1;
idxEnd = idxStart;
}
return floatArr;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment